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"
15 #include "llvm/ConstantVals.h"
19 // TargetData Hack: Eventually we will have annotations given to us by the
20 // backend so that we know stuff about type size and alignments. For now
21 // though, just use this, because it happens to match the model that GCC uses.
23 // FIXME: This should use annotations
25 extern const TargetData TD;
27 static int getConstantValue(const ConstantInt *CPI) {
28 if (const ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPI))
29 return CSI->getValue();
30 return cast<ConstantUInt>(CPI)->getValue();
34 // getPointedToComposite - If the argument is a pointer type, and the pointed to
35 // value is a composite type, return the composite type, else return null.
37 static inline const CompositeType *getPointedToComposite(const Type *Ty) {
38 const PointerType *PT = dyn_cast<PointerType>(Ty);
39 return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0;
43 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
44 // with a value, then remove and delete the original instruction.
46 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
47 BasicBlock::iterator &BI, Value *V);
49 // ReplaceInstWithInst - Replace the instruction specified by BI with the
50 // instruction specified by I. The original instruction is deleted and BI is
51 // updated to point to the new instruction.
53 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
54 BasicBlock::iterator &BI, Instruction *I);
56 void ReplaceInstWithInst(Instruction *From, Instruction *To);
59 // ConvertableToGEP - This function returns true if the specified value V is
60 // a valid index into a pointer of type Ty. If it is valid, Idx is filled in
61 // with the values that would be appropriate to make this a getelementptr
62 // instruction. The type returned is the root type that the GEP would point
63 // to if it were synthesized with this operands.
65 // If BI is nonnull, cast instructions are inserted as appropriate for the
66 // arguments of the getelementptr.
68 const Type *ConvertableToGEP(const Type *Ty, Value *V, vector<Value*> &Indices,
69 BasicBlock::iterator *BI = 0);
72 // ------------- Expression Conversion ---------------------
74 typedef map<const Value*, const Type*> ValueTypeCache;
76 struct ValueMapCache {
77 // Operands mapped - Contains an entry if the first value (the user) has had
78 // the second value (the operand) mapped already.
80 set<const User*> OperandsMapped;
82 // Expression Map - Contains an entry from the old value to the new value of
83 // an expression that has been converted over.
85 map<const Value *, Value *> ExprMap;
86 typedef map<const Value *, Value *> ExprMapTy;
90 bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map);
91 Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC);
93 // ValueConvertableToType - Return true if it is possible
94 bool ValueConvertableToType(Value *V, const Type *Ty,
95 ValueTypeCache &ConvertedTypes);
97 void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC);
100 //===----------------------------------------------------------------------===//
101 // ValueHandle Class - Smart pointer that occupies a slot on the users USE list
102 // that prevents it from being destroyed. This "looks" like an Instruction
103 // with Opcode UserOp1.
105 class ValueHandle : public Instruction {
106 ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT
107 ValueMapCache &Cache;
109 ValueHandle(ValueMapCache &VMC, Value *V);
112 virtual Instruction *clone() const { abort(); return 0; }
114 virtual const char *getOpcodeName() const {
115 return "ValueHandle";
118 // Methods for support type inquiry through isa, cast, and dyn_cast:
119 static inline bool classof(const ValueHandle *) { return true; }
120 static inline bool classof(const Instruction *I) {
121 return (I->getOpcode() == Instruction::UserOp1);
123 static inline bool classof(const Value *V) {
124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
128 // getStructOffsetType - Return a vector of offsets that are to be used to index
129 // into the specified struct type to get as close as possible to index as we
130 // can. Note that it is possible that we cannot get exactly to Offset, in which
131 // case we update offset to be the offset we actually obtained. The resultant
132 // leaf type is returned.
134 // If StopEarly is set to true (the default), the first object with the
135 // specified type is returned, even if it is a struct type itself. In this
136 // case, this routine will not drill down to the leaf type. Set StopEarly to
137 // false if you want a leaf
139 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
140 vector<Value*> &Offsets,
141 bool StopEarly = true);