1 //===-- TransformInternals.h - Shared functions for Transforms --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file declares shared functions used by the different components
11 // of the Transforms library.
13 //===----------------------------------------------------------------------===//
15 #ifndef TRANSFORM_INTERNALS_H
16 #define TRANSFORM_INTERNALS_H
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Constants.h"
27 static inline int64_t getConstantValue(const ConstantInt *CPI) {
28 return (int64_t)cast<ConstantInt>(CPI)->getRawValue();
32 // getPointedToComposite - If the argument is a pointer type, and the pointed to
33 // value is a composite type, return the composite type, else return null.
35 static inline const CompositeType *getPointedToComposite(const Type *Ty) {
36 const PointerType *PT = dyn_cast<PointerType>(Ty);
37 return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0;
40 // ConvertibleToGEP - This function returns true if the specified value V is
41 // a valid index into a pointer of type Ty. If it is valid, Idx is filled in
42 // with the values that would be appropriate to make this a getelementptr
43 // instruction. The type returned is the root type that the GEP would point
44 // to if it were synthesized with this operands.
46 // If BI is nonnull, cast instructions are inserted as appropriate for the
47 // arguments of the getelementptr.
49 const Type *ConvertibleToGEP(const Type *Ty, Value *V,
50 std::vector<Value*> &Indices,
52 BasicBlock::iterator *BI = 0);
55 //===----------------------------------------------------------------------===//
56 // ValueHandle Class - Smart pointer that occupies a slot on the users USE list
57 // that prevents it from being destroyed. This "looks" like an Instruction
58 // with Opcode UserOp1.
61 class ValueHandle : public Instruction {
65 ValueHandle(ValueMapCache &VMC, Value *V);
66 ValueHandle(const ValueHandle &);
69 virtual Instruction *clone() const { abort(); return 0; }
71 virtual const char *getOpcodeName() const {
75 inline bool operator<(const ValueHandle &VH) const {
76 return getOperand(0) < VH.getOperand(0);
79 // Methods for support type inquiry through isa, cast, and dyn_cast:
80 static inline bool classof(const ValueHandle *) { return true; }
81 static inline bool classof(const Instruction *I) {
82 return (I->getOpcode() == Instruction::UserOp1);
84 static inline bool classof(const Value *V) {
85 return isa<Instruction>(V) && classof(cast<Instruction>(V));
90 // ------------- Expression Conversion ---------------------
92 typedef std::map<const Value*, const Type*> ValueTypeCache;
96 // Operands mapped - Contains an entry if the first value (the user) has had
97 // the second value (the operand) mapped already.
99 std::set<const User*> OperandsMapped;
101 // Expression Map - Contains an entry from the old value to the new value of
102 // an expression that has been converted over.
104 std::map<const Value *, Value *> ExprMap;
105 typedef std::map<const Value *, Value *> ExprMapTy;
107 // Cast Map - Cast instructions can have their source and destination values
108 // changed independently for each part. Because of this, our old naive
109 // implementation would create a TWO new cast instructions, which would cause
110 // all kinds of problems. Here we keep track of the newly allocated casts, so
111 // that we only create one for a particular instruction.
113 std::set<ValueHandle> NewCasts;
117 bool ExpressionConvertibleToType(Value *V, const Type *Ty, ValueTypeCache &Map,
118 const TargetData &TD);
119 Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC,
120 const TargetData &TD);
122 // ValueConvertibleToType - Return true if it is possible
123 bool ValueConvertibleToType(Value *V, const Type *Ty,
124 ValueTypeCache &ConvertedTypes,
125 const TargetData &TD);
127 void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC,
128 const TargetData &TD);
131 // getStructOffsetType - Return a vector of offsets that are to be used to index
132 // into the specified struct type to get as close as possible to index as we
133 // can. Note that it is possible that we cannot get exactly to Offset, in which
134 // case we update offset to be the offset we actually obtained. The resultant
135 // leaf type is returned.
137 // If StopEarly is set to true (the default), the first object with the
138 // specified type is returned, even if it is a struct type itself. In this
139 // case, this routine will not drill down to the leaf type. Set StopEarly to
140 // false if you want a leaf
142 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
143 std::vector<Value*> &Offsets,
144 const TargetData &TD, bool StopEarly = true);
146 } // End llvm namespace