blob: 4f92dc4eda8db3a7c6e1bdb3d80fbaf237a810f5 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- TransformInternals.h - Shared functions for Transforms --*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner59cd9f12001-11-04 23:24:06 +00009//
10// This header file declares shared functions used by the different components
11// of the Transforms library.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef TRANSFORM_INTERNALS_H
16#define TRANSFORM_INTERNALS_H
17
18#include "llvm/BasicBlock.h"
19#include "llvm/Target/TargetData.h"
Chris Lattnerc0b90e72001-11-08 20:19:56 +000020#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000021#include "llvm/Constants.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000022#include <map>
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000023#include <set>
Chris Lattner59cd9f12001-11-04 23:24:06 +000024
Chris Lattner8e2e5f72002-09-16 18:32:33 +000025static inline int64_t getConstantValue(const ConstantInt *CPI) {
Chris Lattnerc07736a2003-07-23 15:22:26 +000026 return (int64_t)cast<ConstantInt>(CPI)->getRawValue();
Chris Lattner491b29f2001-11-26 17:00:13 +000027}
Chris Lattner59cd9f12001-11-04 23:24:06 +000028
29
Chris Lattner491b29f2001-11-26 17:00:13 +000030// getPointedToComposite - If the argument is a pointer type, and the pointed to
31// value is a composite type, return the composite type, else return null.
Chris Lattnerc0b90e72001-11-08 20:19:56 +000032//
Chris Lattner491b29f2001-11-26 17:00:13 +000033static inline const CompositeType *getPointedToComposite(const Type *Ty) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +000034 const PointerType *PT = dyn_cast<PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +000035 return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0;
Chris Lattnerc0b90e72001-11-08 20:19:56 +000036}
37
Misha Brukmanf117cc92003-05-20 18:45:36 +000038// ConvertibleToGEP - This function returns true if the specified value V is
Chris Lattner491b29f2001-11-26 17:00:13 +000039// a valid index into a pointer of type Ty. If it is valid, Idx is filled in
40// with the values that would be appropriate to make this a getelementptr
41// instruction. The type returned is the root type that the GEP would point
42// to if it were synthesized with this operands.
43//
44// If BI is nonnull, cast instructions are inserted as appropriate for the
45// arguments of the getelementptr.
46//
Misha Brukmanf117cc92003-05-20 18:45:36 +000047const Type *ConvertibleToGEP(const Type *Ty, Value *V,
Chris Lattner697954c2002-01-20 22:54:45 +000048 std::vector<Value*> &Indices,
Chris Lattner16125fb2003-04-24 18:25:27 +000049 const TargetData &TD,
Chris Lattner491b29f2001-11-26 17:00:13 +000050 BasicBlock::iterator *BI = 0);
51
52
Chris Lattnerb1b42622002-07-17 17:11:33 +000053//===----------------------------------------------------------------------===//
54// ValueHandle Class - Smart pointer that occupies a slot on the users USE list
55// that prevents it from being destroyed. This "looks" like an Instruction
56// with Opcode UserOp1.
57//
58class ValueMapCache;
59class ValueHandle : public Instruction {
60 ValueMapCache &Cache;
61public:
62 ValueHandle(ValueMapCache &VMC, Value *V);
63 ValueHandle(const ValueHandle &);
64 ~ValueHandle();
65
66 virtual Instruction *clone() const { abort(); return 0; }
67
68 virtual const char *getOpcodeName() const {
69 return "ValueHandle";
70 }
71
72 inline bool operator<(const ValueHandle &VH) const {
73 return getOperand(0) < VH.getOperand(0);
74 }
75
76 // Methods for support type inquiry through isa, cast, and dyn_cast:
77 static inline bool classof(const ValueHandle *) { return true; }
78 static inline bool classof(const Instruction *I) {
79 return (I->getOpcode() == Instruction::UserOp1);
80 }
81 static inline bool classof(const Value *V) {
82 return isa<Instruction>(V) && classof(cast<Instruction>(V));
83 }
84};
85
86
Chris Lattner59cd9f12001-11-04 23:24:06 +000087// ------------- Expression Conversion ---------------------
88
Chris Lattnerb1b42622002-07-17 17:11:33 +000089typedef std::map<const Value*, const Type*> ValueTypeCache;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000090
91struct ValueMapCache {
92 // Operands mapped - Contains an entry if the first value (the user) has had
93 // the second value (the operand) mapped already.
94 //
Chris Lattner697954c2002-01-20 22:54:45 +000095 std::set<const User*> OperandsMapped;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000096
97 // Expression Map - Contains an entry from the old value to the new value of
98 // an expression that has been converted over.
99 //
Chris Lattner697954c2002-01-20 22:54:45 +0000100 std::map<const Value *, Value *> ExprMap;
101 typedef std::map<const Value *, Value *> ExprMapTy;
Chris Lattnerb1b42622002-07-17 17:11:33 +0000102
103 // Cast Map - Cast instructions can have their source and destination values
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000104 // changed independently for each part. Because of this, our old naive
Chris Lattnerb1b42622002-07-17 17:11:33 +0000105 // implementation would create a TWO new cast instructions, which would cause
106 // all kinds of problems. Here we keep track of the newly allocated casts, so
107 // that we only create one for a particular instruction.
108 //
109 std::set<ValueHandle> NewCasts;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000110};
Chris Lattner59cd9f12001-11-04 23:24:06 +0000111
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000112
Misha Brukmanf117cc92003-05-20 18:45:36 +0000113bool ExpressionConvertibleToType(Value *V, const Type *Ty, ValueTypeCache &Map,
Chris Lattner16125fb2003-04-24 18:25:27 +0000114 const TargetData &TD);
115Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC,
116 const TargetData &TD);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000117
Misha Brukmanf117cc92003-05-20 18:45:36 +0000118// ValueConvertibleToType - Return true if it is possible
119bool ValueConvertibleToType(Value *V, const Type *Ty,
Chris Lattner16125fb2003-04-24 18:25:27 +0000120 ValueTypeCache &ConvertedTypes,
121 const TargetData &TD);
Chris Lattner59cd9f12001-11-04 23:24:06 +0000122
Chris Lattner16125fb2003-04-24 18:25:27 +0000123void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC,
124 const TargetData &TD);
Chris Lattner59cd9f12001-11-04 23:24:06 +0000125
126
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000127// getStructOffsetType - Return a vector of offsets that are to be used to index
128// into the specified struct type to get as close as possible to index as we
129// can. Note that it is possible that we cannot get exactly to Offset, in which
130// case we update offset to be the offset we actually obtained. The resultant
131// leaf type is returned.
132//
133// If StopEarly is set to true (the default), the first object with the
134// specified type is returned, even if it is a struct type itself. In this
135// case, this routine will not drill down to the leaf type. Set StopEarly to
136// false if you want a leaf
137//
138const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
Chris Lattner697954c2002-01-20 22:54:45 +0000139 std::vector<Value*> &Offsets,
Chris Lattner16125fb2003-04-24 18:25:27 +0000140 const TargetData &TD, bool StopEarly = true);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000141
Chris Lattner59cd9f12001-11-04 23:24:06 +0000142#endif