LLVM API Documentation
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 00041 //===----------------------------------------------------------------------===// 00042 // ValueHandle Class - Smart pointer that occupies a slot on the users USE list 00043 // that prevents it from being destroyed. This "looks" like an Instruction 00044 // with Opcode UserOp1. 00045 // 00046 class ValueMapCache; 00047 class ValueHandle : public Instruction { 00048 Use Op; 00049 ValueMapCache &Cache; 00050 public: 00051 ValueHandle(ValueMapCache &VMC, Value *V); 00052 ValueHandle(const ValueHandle &); 00053 ~ValueHandle(); 00054 00055 virtual Instruction *clone() const { abort(); return 0; } 00056 00057 virtual const char *getOpcodeName() const { 00058 return "ValueHandle"; 00059 } 00060 00061 inline bool operator<(const ValueHandle &VH) const { 00062 return getOperand(0) < VH.getOperand(0); 00063 } 00064 00065 // Methods for support type inquiry through isa, cast, and dyn_cast: 00066 static inline bool classof(const ValueHandle *) { return true; } 00067 static inline bool classof(const Instruction *I) { 00068 return (I->getOpcode() == Instruction::UserOp1); 00069 } 00070 static inline bool classof(const Value *V) { 00071 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00072 } 00073 }; 00074 00075 00076 // ------------- Expression Conversion --------------------- 00077 00078 typedef std::map<const Value*, const Type*> ValueTypeCache; 00079 00080 class ValueMapCache { 00081 public: 00082 // Operands mapped - Contains an entry if the first value (the user) has had 00083 // the second value (the operand) mapped already. 00084 // 00085 std::set<const User*> OperandsMapped; 00086 00087 // Expression Map - Contains an entry from the old value to the new value of 00088 // an expression that has been converted over. 00089 // 00090 std::map<const Value *, Value *> ExprMap; 00091 typedef std::map<const Value *, Value *> ExprMapTy; 00092 00093 // Cast Map - Cast instructions can have their source and destination values 00094 // changed independently for each part. Because of this, our old naive 00095 // implementation would create a TWO new cast instructions, which would cause 00096 // all kinds of problems. Here we keep track of the newly allocated casts, so 00097 // that we only create one for a particular instruction. 00098 // 00099 std::set<ValueHandle> NewCasts; 00100 }; 00101 00102 00103 bool ExpressionConvertibleToType(Value *V, const Type *Ty, ValueTypeCache &Map, 00104 const TargetData &TD); 00105 Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC, 00106 const TargetData &TD); 00107 00108 // ValueConvertibleToType - Return true if it is possible 00109 bool ValueConvertibleToType(Value *V, const Type *Ty, 00110 ValueTypeCache &ConvertedTypes, 00111 const TargetData &TD); 00112 00113 void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC, 00114 const TargetData &TD); 00115 00116 00117 // getStructOffsetType - Return a vector of offsets that are to be used to index 00118 // into the specified struct type to get as close as possible to index as we 00119 // can. Note that it is possible that we cannot get exactly to Offset, in which 00120 // case we update offset to be the offset we actually obtained. The resultant 00121 // leaf type is returned. 00122 // 00123 // If StopEarly is set to true (the default), the first object with the 00124 // specified type is returned, even if it is a struct type itself. In this 00125 // case, this routine will not drill down to the leaf type. Set StopEarly to 00126 // false if you want a leaf 00127 // 00128 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, 00129 std::vector<Value*> &Offsets, 00130 const TargetData &TD, bool StopEarly = true); 00131 00132 } // End llvm namespace 00133 00134 #endif