LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TransformInternals.h

Go to the documentation of this file.
00001 //===-- TransformInternals.h - Shared functions for Transforms --*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This header file declares shared functions used by the different components
00011 //  of the Transforms library.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef TRANSFORM_INTERNALS_H
00016 #define TRANSFORM_INTERNALS_H
00017 
00018 #include "llvm/BasicBlock.h"
00019 #include "llvm/Target/TargetData.h"
00020 #include "llvm/DerivedTypes.h"
00021 #include "llvm/Constants.h"
00022 #include <map>
00023 #include <set>
00024 
00025 namespace llvm {
00026 
00027 static inline int64_t getConstantValue(const ConstantInt *CPI) {
00028   return (int64_t)cast<ConstantInt>(CPI)->getRawValue();
00029 }
00030 
00031 
00032 // getPointedToComposite - If the argument is a pointer type, and the pointed to
00033 // value is a composite type, return the composite type, else return null.
00034 //
00035 static inline const CompositeType *getPointedToComposite(const Type *Ty) {
00036   const PointerType *PT = dyn_cast<PointerType>(Ty);
00037   return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0;
00038 }
00039 
00040 // ConvertibleToGEP - This function returns true if the specified value V is
00041 // a valid index into a pointer of type Ty.  If it is valid, Idx is filled in
00042 // with the values that would be appropriate to make this a getelementptr
00043 // instruction.  The type returned is the root type that the GEP would point
00044 // to if it were synthesized with this operands.
00045 //
00046 // If BI is nonnull, cast instructions are inserted as appropriate for the
00047 // arguments of the getelementptr.
00048 //
00049 const Type *ConvertibleToGEP(const Type *Ty, Value *V,
00050                              std::vector<Value*> &Indices,
00051                              const TargetData &TD,
00052                              BasicBlock::iterator *BI = 0);
00053 
00054 
00055 //===----------------------------------------------------------------------===//
00056 //  ValueHandle Class - Smart pointer that occupies a slot on the users USE list
00057 //  that prevents it from being destroyed.  This "looks" like an Instruction
00058 //  with Opcode UserOp1.
00059 // 
00060 class ValueMapCache;
00061 class ValueHandle : public Instruction {
00062   ValueMapCache &Cache;
00063 public:
00064   ValueHandle(ValueMapCache &VMC, Value *V);
00065   ValueHandle(const ValueHandle &);
00066   ~ValueHandle();
00067 
00068   virtual Instruction *clone() const { abort(); return 0; }
00069 
00070   virtual const char *getOpcodeName() const {
00071     return "ValueHandle";
00072   }
00073 
00074   inline bool operator<(const ValueHandle &VH) const {
00075     return getOperand(0) < VH.getOperand(0);
00076   }
00077 
00078   // Methods for support type inquiry through isa, cast, and dyn_cast:
00079   static inline bool classof(const ValueHandle *) { return true; }
00080   static inline bool classof(const Instruction *I) {
00081     return (I->getOpcode() == Instruction::UserOp1);
00082   }
00083   static inline bool classof(const Value *V) {
00084     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00085   }
00086 };
00087 
00088 
00089 // ------------- Expression Conversion ---------------------
00090 
00091 typedef std::map<const Value*, const Type*> ValueTypeCache;
00092 
00093 class ValueMapCache {
00094 public:
00095   // Operands mapped - Contains an entry if the first value (the user) has had
00096   // the second value (the operand) mapped already.
00097   //
00098   std::set<const User*> OperandsMapped;
00099 
00100   // Expression Map - Contains an entry from the old value to the new value of
00101   // an expression that has been converted over.
00102   //
00103   std::map<const Value *, Value *> ExprMap;
00104   typedef std::map<const Value *, Value *> ExprMapTy;
00105 
00106   // Cast Map - Cast instructions can have their source and destination values
00107   // changed independently for each part.  Because of this, our old naive
00108   // implementation would create a TWO new cast instructions, which would cause
00109   // all kinds of problems.  Here we keep track of the newly allocated casts, so
00110   // that we only create one for a particular instruction.
00111   //
00112   std::set<ValueHandle> NewCasts;
00113 };
00114 
00115 
00116 bool ExpressionConvertibleToType(Value *V, const Type *Ty, ValueTypeCache &Map,
00117                                  const TargetData &TD);
00118 Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC,
00119                                const TargetData &TD);
00120 
00121 // ValueConvertibleToType - Return true if it is possible
00122 bool ValueConvertibleToType(Value *V, const Type *Ty,
00123                             ValueTypeCache &ConvertedTypes,
00124                             const TargetData &TD);
00125 
00126 void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC,
00127                            const TargetData &TD);
00128 
00129 
00130 // getStructOffsetType - Return a vector of offsets that are to be used to index
00131 // into the specified struct type to get as close as possible to index as we
00132 // can.  Note that it is possible that we cannot get exactly to Offset, in which
00133 // case we update offset to be the offset we actually obtained.  The resultant
00134 // leaf type is returned.
00135 //
00136 // If StopEarly is set to true (the default), the first object with the
00137 // specified type is returned, even if it is a struct type itself.  In this
00138 // case, this routine will not drill down to the leaf type.  Set StopEarly to
00139 // false if you want a leaf
00140 //
00141 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
00142                                 std::vector<Value*> &Offsets,
00143                                 const TargetData &TD, bool StopEarly = true);
00144 
00145 } // End llvm namespace
00146 
00147 #endif