blob: 6680d02b86551e9013a4ab47d0e7502132cd8b9d [file] [log] [blame]
Chris Lattner59cd9f12001-11-04 23:24:06 +00001//===-- TransformInternals.h - Shared functions for Transforms ---*- C++ -*--=//
2//
3// This header file declares shared functions used by the different components
4// of the Transforms library.
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef TRANSFORM_INTERNALS_H
9#define TRANSFORM_INTERNALS_H
10
11#include "llvm/BasicBlock.h"
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000012#include "llvm/Instruction.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000013#include "llvm/Target/TargetData.h"
Chris Lattnerc0b90e72001-11-08 20:19:56 +000014#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000015#include "llvm/Constants.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000016#include <map>
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000017#include <set>
Chris Lattner59cd9f12001-11-04 23:24:06 +000018
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.
22//
23// FIXME: This should use annotations
24//
25extern const TargetData TD;
26
Chris Lattner697954c2002-01-20 22:54:45 +000027static inline int getConstantValue(const ConstantInt *CPI) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000028 if (const ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPI))
Chris Lattner4a94a702002-04-16 22:10:52 +000029 return (int)CSI->getValue();
30 return (int)cast<ConstantUInt>(CPI)->getValue();
Chris Lattner491b29f2001-11-26 17:00:13 +000031}
Chris Lattner59cd9f12001-11-04 23:24:06 +000032
33
Chris Lattner491b29f2001-11-26 17:00:13 +000034// 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.
Chris Lattnerc0b90e72001-11-08 20:19:56 +000036//
Chris Lattner491b29f2001-11-26 17:00:13 +000037static inline const CompositeType *getPointedToComposite(const Type *Ty) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +000038 const PointerType *PT = dyn_cast<PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +000039 return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0;
Chris Lattnerc0b90e72001-11-08 20:19:56 +000040}
41
Chris Lattner491b29f2001-11-26 17:00:13 +000042// ConvertableToGEP - This function returns true if the specified value V is
43// a valid index into a pointer of type Ty. If it is valid, Idx is filled in
44// with the values that would be appropriate to make this a getelementptr
45// instruction. The type returned is the root type that the GEP would point
46// to if it were synthesized with this operands.
47//
48// If BI is nonnull, cast instructions are inserted as appropriate for the
49// arguments of the getelementptr.
50//
Chris Lattner697954c2002-01-20 22:54:45 +000051const Type *ConvertableToGEP(const Type *Ty, Value *V,
52 std::vector<Value*> &Indices,
Chris Lattner491b29f2001-11-26 17:00:13 +000053 BasicBlock::iterator *BI = 0);
54
55
Chris Lattner59cd9f12001-11-04 23:24:06 +000056// ------------- Expression Conversion ---------------------
57
Chris Lattner697954c2002-01-20 22:54:45 +000058typedef std::map<const Value*, const Type*> ValueTypeCache;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000059
60struct ValueMapCache {
61 // Operands mapped - Contains an entry if the first value (the user) has had
62 // the second value (the operand) mapped already.
63 //
Chris Lattner697954c2002-01-20 22:54:45 +000064 std::set<const User*> OperandsMapped;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000065
66 // Expression Map - Contains an entry from the old value to the new value of
67 // an expression that has been converted over.
68 //
Chris Lattner697954c2002-01-20 22:54:45 +000069 std::map<const Value *, Value *> ExprMap;
70 typedef std::map<const Value *, Value *> ExprMapTy;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000071};
Chris Lattner59cd9f12001-11-04 23:24:06 +000072
Chris Lattnerc0b90e72001-11-08 20:19:56 +000073
74bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map);
75Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC);
76
Chris Lattner491b29f2001-11-26 17:00:13 +000077// ValueConvertableToType - Return true if it is possible
78bool ValueConvertableToType(Value *V, const Type *Ty,
79 ValueTypeCache &ConvertedTypes);
Chris Lattner59cd9f12001-11-04 23:24:06 +000080
Chris Lattner491b29f2001-11-26 17:00:13 +000081void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC);
Chris Lattner59cd9f12001-11-04 23:24:06 +000082
83
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000084//===----------------------------------------------------------------------===//
85// ValueHandle Class - Smart pointer that occupies a slot on the users USE list
86// that prevents it from being destroyed. This "looks" like an Instruction
87// with Opcode UserOp1.
88//
89class ValueHandle : public Instruction {
90 ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT
Chris Lattnerce22ec12001-11-13 05:01:36 +000091 ValueMapCache &Cache;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000092public:
Chris Lattnerce22ec12001-11-13 05:01:36 +000093 ValueHandle(ValueMapCache &VMC, Value *V);
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000094 ~ValueHandle();
95
96 virtual Instruction *clone() const { abort(); return 0; }
97
98 virtual const char *getOpcodeName() const {
99 return "ValueHandle";
100 }
101
102 // Methods for support type inquiry through isa, cast, and dyn_cast:
103 static inline bool classof(const ValueHandle *) { return true; }
104 static inline bool classof(const Instruction *I) {
105 return (I->getOpcode() == Instruction::UserOp1);
106 }
107 static inline bool classof(const Value *V) {
108 return isa<Instruction>(V) && classof(cast<Instruction>(V));
109 }
110};
111
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000112// getStructOffsetType - Return a vector of offsets that are to be used to index
113// into the specified struct type to get as close as possible to index as we
114// can. Note that it is possible that we cannot get exactly to Offset, in which
115// case we update offset to be the offset we actually obtained. The resultant
116// leaf type is returned.
117//
118// If StopEarly is set to true (the default), the first object with the
119// specified type is returned, even if it is a struct type itself. In this
120// case, this routine will not drill down to the leaf type. Set StopEarly to
121// false if you want a leaf
122//
123const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
Chris Lattner697954c2002-01-20 22:54:45 +0000124 std::vector<Value*> &Offsets,
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000125 bool StopEarly = true);
126
Chris Lattner59cd9f12001-11-04 23:24:06 +0000127#endif