blob: 9f6eb7954bc59ecb20aefccdb46bed355580b691 [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"
12#include "llvm/Target/TargetData.h"
Chris Lattnerc0b90e72001-11-08 20:19:56 +000013#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000015#include <map>
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000016#include <set>
Chris Lattner59cd9f12001-11-04 23:24:06 +000017
Chris Lattner8e2e5f72002-09-16 18:32:33 +000018static inline int64_t getConstantValue(const ConstantInt *CPI) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000019 if (const ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPI))
Chris Lattner8e2e5f72002-09-16 18:32:33 +000020 return CSI->getValue();
21 return (int64_t)cast<ConstantUInt>(CPI)->getValue();
Chris Lattner491b29f2001-11-26 17:00:13 +000022}
Chris Lattner59cd9f12001-11-04 23:24:06 +000023
24
Chris Lattner491b29f2001-11-26 17:00:13 +000025// getPointedToComposite - If the argument is a pointer type, and the pointed to
26// value is a composite type, return the composite type, else return null.
Chris Lattnerc0b90e72001-11-08 20:19:56 +000027//
Chris Lattner491b29f2001-11-26 17:00:13 +000028static inline const CompositeType *getPointedToComposite(const Type *Ty) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +000029 const PointerType *PT = dyn_cast<PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +000030 return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0;
Chris Lattnerc0b90e72001-11-08 20:19:56 +000031}
32
Chris Lattner491b29f2001-11-26 17:00:13 +000033// ConvertableToGEP - This function returns true if the specified value V is
34// a valid index into a pointer of type Ty. If it is valid, Idx is filled in
35// with the values that would be appropriate to make this a getelementptr
36// instruction. The type returned is the root type that the GEP would point
37// to if it were synthesized with this operands.
38//
39// If BI is nonnull, cast instructions are inserted as appropriate for the
40// arguments of the getelementptr.
41//
Chris Lattner697954c2002-01-20 22:54:45 +000042const Type *ConvertableToGEP(const Type *Ty, Value *V,
43 std::vector<Value*> &Indices,
Chris Lattner16125fb2003-04-24 18:25:27 +000044 const TargetData &TD,
Chris Lattner491b29f2001-11-26 17:00:13 +000045 BasicBlock::iterator *BI = 0);
46
47
Chris Lattnerb1b42622002-07-17 17:11:33 +000048//===----------------------------------------------------------------------===//
49// ValueHandle Class - Smart pointer that occupies a slot on the users USE list
50// that prevents it from being destroyed. This "looks" like an Instruction
51// with Opcode UserOp1.
52//
53class ValueMapCache;
54class ValueHandle : public Instruction {
55 ValueMapCache &Cache;
56public:
57 ValueHandle(ValueMapCache &VMC, Value *V);
58 ValueHandle(const ValueHandle &);
59 ~ValueHandle();
60
61 virtual Instruction *clone() const { abort(); return 0; }
62
63 virtual const char *getOpcodeName() const {
64 return "ValueHandle";
65 }
66
67 inline bool operator<(const ValueHandle &VH) const {
68 return getOperand(0) < VH.getOperand(0);
69 }
70
71 // Methods for support type inquiry through isa, cast, and dyn_cast:
72 static inline bool classof(const ValueHandle *) { return true; }
73 static inline bool classof(const Instruction *I) {
74 return (I->getOpcode() == Instruction::UserOp1);
75 }
76 static inline bool classof(const Value *V) {
77 return isa<Instruction>(V) && classof(cast<Instruction>(V));
78 }
79};
80
81
Chris Lattner59cd9f12001-11-04 23:24:06 +000082// ------------- Expression Conversion ---------------------
83
Chris Lattnerb1b42622002-07-17 17:11:33 +000084typedef std::map<const Value*, const Type*> ValueTypeCache;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000085
86struct ValueMapCache {
87 // Operands mapped - Contains an entry if the first value (the user) has had
88 // the second value (the operand) mapped already.
89 //
Chris Lattner697954c2002-01-20 22:54:45 +000090 std::set<const User*> OperandsMapped;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000091
92 // Expression Map - Contains an entry from the old value to the new value of
93 // an expression that has been converted over.
94 //
Chris Lattner697954c2002-01-20 22:54:45 +000095 std::map<const Value *, Value *> ExprMap;
96 typedef std::map<const Value *, Value *> ExprMapTy;
Chris Lattnerb1b42622002-07-17 17:11:33 +000097
98 // Cast Map - Cast instructions can have their source and destination values
99 // changed independantly for each part. Because of this, our old naive
100 // implementation would create a TWO new cast instructions, which would cause
101 // all kinds of problems. Here we keep track of the newly allocated casts, so
102 // that we only create one for a particular instruction.
103 //
104 std::set<ValueHandle> NewCasts;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000105};
Chris Lattner59cd9f12001-11-04 23:24:06 +0000106
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000107
Chris Lattner16125fb2003-04-24 18:25:27 +0000108bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map,
109 const TargetData &TD);
110Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC,
111 const TargetData &TD);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000112
Chris Lattner491b29f2001-11-26 17:00:13 +0000113// ValueConvertableToType - Return true if it is possible
114bool ValueConvertableToType(Value *V, const Type *Ty,
Chris Lattner16125fb2003-04-24 18:25:27 +0000115 ValueTypeCache &ConvertedTypes,
116 const TargetData &TD);
Chris Lattner59cd9f12001-11-04 23:24:06 +0000117
Chris Lattner16125fb2003-04-24 18:25:27 +0000118void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC,
119 const TargetData &TD);
Chris Lattner59cd9f12001-11-04 23:24:06 +0000120
121
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000122// getStructOffsetType - Return a vector of offsets that are to be used to index
123// into the specified struct type to get as close as possible to index as we
124// can. Note that it is possible that we cannot get exactly to Offset, in which
125// case we update offset to be the offset we actually obtained. The resultant
126// leaf type is returned.
127//
128// If StopEarly is set to true (the default), the first object with the
129// specified type is returned, even if it is a struct type itself. In this
130// case, this routine will not drill down to the leaf type. Set StopEarly to
131// false if you want a leaf
132//
133const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
Chris Lattner697954c2002-01-20 22:54:45 +0000134 std::vector<Value*> &Offsets,
Chris Lattner16125fb2003-04-24 18:25:27 +0000135 const TargetData &TD, bool StopEarly = true);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000136
Chris Lattner59cd9f12001-11-04 23:24:06 +0000137#endif