1 //===-- TransformInternals.h - Shared functions for Transforms ---*- C++ -*--=//
3 // This header file declares shared functions used by the different components
4 // of the Transforms library.
6 //===----------------------------------------------------------------------===//
8 #ifndef TRANSFORM_INTERNALS_H
9 #define TRANSFORM_INTERNALS_H
11 #include "llvm/BasicBlock.h"
12 #include "llvm/Instruction.h"
13 #include "llvm/Target/TargetData.h"
14 #include "llvm/DerivedTypes.h"
18 // TargetData Hack: Eventually we will have annotations given to us by the
19 // backend so that we know stuff about type size and alignments. For now
20 // though, just use this, because it happens to match the model that GCC uses.
22 // FIXME: This should use annotations
24 extern const TargetData TD;
26 // losslessCastableTypes - Return true if the types are bitwise equivalent.
27 // This predicate returns true if it is possible to cast from one type to
28 // another without gaining or losing precision, or altering the bits in any way.
30 bool losslessCastableTypes(const Type *T1, const Type *T2);
33 // isFirstClassType - Return true if a value of the specified type can be held
36 static inline bool isFirstClassType(const Type *Ty) {
37 return Ty->isPrimitiveType() || Ty->isPointerType();
40 // getPointedToStruct - If the argument is a pointer type, and the pointed to
41 // value is a struct type, return the struct type, else return null.
43 static inline const StructType *getPointedToStruct(const Type *Ty) {
44 const PointerType *PT = dyn_cast<PointerType>(Ty);
45 return PT ? dyn_cast<StructType>(PT->getValueType()) : 0;
49 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
50 // with a value, then remove and delete the original instruction.
52 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
53 BasicBlock::iterator &BI, Value *V);
55 // ReplaceInstWithInst - Replace the instruction specified by BI with the
56 // instruction specified by I. The original instruction is deleted and BI is
57 // updated to point to the new instruction.
59 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
60 BasicBlock::iterator &BI, Instruction *I);
63 // ------------- Expression Conversion ---------------------
65 typedef map<const Value*, const Type*> ValueTypeCache;
67 struct ValueMapCache {
68 // Operands mapped - Contains an entry if the first value (the user) has had
69 // the second value (the operand) mapped already.
71 set<const User*> OperandsMapped;
73 // Expression Map - Contains an entry from the old value to the new value of
74 // an expression that has been converted over.
76 map<const Value *, Value *> ExprMap;
77 typedef map<const Value *, Value *> ExprMapTy;
81 bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map);
82 Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC);
84 // RetValConvertableToType - Return true if it is possible
85 bool RetValConvertableToType(Value *V, const Type *Ty,
86 ValueTypeCache &ConvertedTypes);
88 void ConvertUsersType(Value *V, Value *NewVal, ValueMapCache &VMC);
91 //===----------------------------------------------------------------------===//
92 // ValueHandle Class - Smart pointer that occupies a slot on the users USE list
93 // that prevents it from being destroyed. This "looks" like an Instruction
94 // with Opcode UserOp1.
96 class ValueHandle : public Instruction {
97 ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT
100 ValueHandle(ValueMapCache &VMC, Value *V);
103 virtual Instruction *clone() const { abort(); return 0; }
105 virtual const char *getOpcodeName() const {
106 return "ValueHandle";
109 // Methods for support type inquiry through isa, cast, and dyn_cast:
110 static inline bool classof(const ValueHandle *) { return true; }
111 static inline bool classof(const Instruction *I) {
112 return (I->getOpcode() == Instruction::UserOp1);
114 static inline bool classof(const Value *V) {
115 return isa<Instruction>(V) && classof(cast<Instruction>(V));
119 // getStructOffsetType - Return a vector of offsets that are to be used to index
120 // into the specified struct type to get as close as possible to index as we
121 // can. Note that it is possible that we cannot get exactly to Offset, in which
122 // case we update offset to be the offset we actually obtained. The resultant
123 // leaf type is returned.
125 // If StopEarly is set to true (the default), the first object with the
126 // specified type is returned, even if it is a struct type itself. In this
127 // case, this routine will not drill down to the leaf type. Set StopEarly to
128 // false if you want a leaf
130 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
131 vector<ConstPoolVal*> &Offsets,
132 bool StopEarly = true);