blob: 7797ed15ead6c627d987c61a6585740e13cccd00 [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 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
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.
21//
22// FIXME: This should use annotations
23//
24extern const TargetData TD;
25
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.
29//
30bool losslessCastableTypes(const Type *T1, const Type *T2);
31
32
Chris Lattnere34443d2001-11-06 08:34:29 +000033// isFirstClassType - Return true if a value of the specified type can be held
34// in a register.
35//
36static inline bool isFirstClassType(const Type *Ty) {
37 return Ty->isPrimitiveType() || Ty->isPointerType();
38}
39
Chris Lattnerc0b90e72001-11-08 20:19:56 +000040// 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.
42//
43static inline const StructType *getPointedToStruct(const Type *Ty) {
44 const PointerType *PT = dyn_cast<PointerType>(Ty);
45 return PT ? dyn_cast<StructType>(PT->getValueType()) : 0;
46}
47
Chris Lattnere34443d2001-11-06 08:34:29 +000048
Chris Lattner59cd9f12001-11-04 23:24:06 +000049// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
50// with a value, then remove and delete the original instruction.
51//
52void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
53 BasicBlock::iterator &BI, Value *V);
54
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.
58//
59void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
60 BasicBlock::iterator &BI, Instruction *I);
61
62
63// ------------- Expression Conversion ---------------------
64
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000065typedef map<const Value*, const Type*> ValueTypeCache;
66
67struct ValueMapCache {
68 // Operands mapped - Contains an entry if the first value (the user) has had
69 // the second value (the operand) mapped already.
70 //
Chris Lattnere34443d2001-11-06 08:34:29 +000071 set<const User*> OperandsMapped;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000072
73 // Expression Map - Contains an entry from the old value to the new value of
74 // an expression that has been converted over.
75 //
76 map<const Value *, Value *> ExprMap;
77 typedef map<const Value *, Value *> ExprMapTy;
78};
Chris Lattner59cd9f12001-11-04 23:24:06 +000079
Chris Lattnerc0b90e72001-11-08 20:19:56 +000080
81bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map);
82Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC);
83
Chris Lattner59cd9f12001-11-04 23:24:06 +000084// RetValConvertableToType - Return true if it is possible
85bool RetValConvertableToType(Value *V, const Type *Ty,
86 ValueTypeCache &ConvertedTypes);
87
88void ConvertUsersType(Value *V, Value *NewVal, ValueMapCache &VMC);
89
90
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000091//===----------------------------------------------------------------------===//
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.
95//
96class ValueHandle : public Instruction {
97 ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT
Chris Lattnerce22ec12001-11-13 05:01:36 +000098 ValueMapCache &Cache;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000099public:
Chris Lattnerce22ec12001-11-13 05:01:36 +0000100 ValueHandle(ValueMapCache &VMC, Value *V);
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000101 ~ValueHandle();
102
103 virtual Instruction *clone() const { abort(); return 0; }
104
105 virtual const char *getOpcodeName() const {
106 return "ValueHandle";
107 }
108
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);
113 }
114 static inline bool classof(const Value *V) {
115 return isa<Instruction>(V) && classof(cast<Instruction>(V));
116 }
117};
118
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000119// 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.
124//
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
129//
130const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
131 vector<ConstPoolVal*> &Offsets,
132 bool StopEarly = true);
133
Chris Lattner59cd9f12001-11-04 23:24:06 +0000134#endif