blob: ba3bd5a7a701bc808efaa0bfb3f1ab366ffe28c4 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- TransformInternals.h - Shared functions for Transforms --*- C++ -*-===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// 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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
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
Brian Gaeked0fde302003-11-11 22:41:34 +000025namespace llvm {
26
Chris Lattner8e2e5f72002-09-16 18:32:33 +000027static inline int64_t getConstantValue(const ConstantInt *CPI) {
Chris Lattnerc07736a2003-07-23 15:22:26 +000028 return (int64_t)cast<ConstantInt>(CPI)->getRawValue();
Chris Lattner491b29f2001-11-26 17:00:13 +000029}
Chris Lattner59cd9f12001-11-04 23:24:06 +000030
31
Chris Lattner491b29f2001-11-26 17:00:13 +000032// 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.
Chris Lattnerc0b90e72001-11-08 20:19:56 +000034//
Chris Lattner491b29f2001-11-26 17:00:13 +000035static inline const CompositeType *getPointedToComposite(const Type *Ty) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +000036 const PointerType *PT = dyn_cast<PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +000037 return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0;
Chris Lattnerc0b90e72001-11-08 20:19:56 +000038}
39
Misha Brukmanf117cc92003-05-20 18:45:36 +000040// ConvertibleToGEP - This function returns true if the specified value V is
Chris Lattner491b29f2001-11-26 17:00:13 +000041// 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.
45//
46// If BI is nonnull, cast instructions are inserted as appropriate for the
47// arguments of the getelementptr.
48//
Misha Brukmanf117cc92003-05-20 18:45:36 +000049const Type *ConvertibleToGEP(const Type *Ty, Value *V,
Chris Lattner697954c2002-01-20 22:54:45 +000050 std::vector<Value*> &Indices,
Chris Lattner16125fb2003-04-24 18:25:27 +000051 const TargetData &TD,
Chris Lattner491b29f2001-11-26 17:00:13 +000052 BasicBlock::iterator *BI = 0);
53
54
Chris Lattnerb1b42622002-07-17 17:11:33 +000055//===----------------------------------------------------------------------===//
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.
Misha Brukmanfd939082005-04-21 23:48:37 +000059//
Chris Lattnerb1b42622002-07-17 17:11:33 +000060class ValueMapCache;
61class ValueHandle : public Instruction {
Chris Lattner5598aa02005-01-29 00:37:36 +000062 Use Op;
Chris Lattnerb1b42622002-07-17 17:11:33 +000063 ValueMapCache &Cache;
64public:
65 ValueHandle(ValueMapCache &VMC, Value *V);
66 ValueHandle(const ValueHandle &);
67 ~ValueHandle();
68
69 virtual Instruction *clone() const { abort(); return 0; }
70
71 virtual const char *getOpcodeName() const {
72 return "ValueHandle";
73 }
74
75 inline bool operator<(const ValueHandle &VH) const {
76 return getOperand(0) < VH.getOperand(0);
77 }
78
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);
83 }
84 static inline bool classof(const Value *V) {
85 return isa<Instruction>(V) && classof(cast<Instruction>(V));
86 }
87};
88
89
Chris Lattner59cd9f12001-11-04 23:24:06 +000090// ------------- Expression Conversion ---------------------
91
Chris Lattnerb1b42622002-07-17 17:11:33 +000092typedef std::map<const Value*, const Type*> ValueTypeCache;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000093
Chris Lattner1fca5ff2004-10-27 16:14:51 +000094class ValueMapCache {
95public:
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000096 // Operands mapped - Contains an entry if the first value (the user) has had
97 // the second value (the operand) mapped already.
98 //
Chris Lattner697954c2002-01-20 22:54:45 +000099 std::set<const User*> OperandsMapped;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000100
101 // Expression Map - Contains an entry from the old value to the new value of
102 // an expression that has been converted over.
103 //
Chris Lattner697954c2002-01-20 22:54:45 +0000104 std::map<const Value *, Value *> ExprMap;
105 typedef std::map<const Value *, Value *> ExprMapTy;
Chris Lattnerb1b42622002-07-17 17:11:33 +0000106
107 // Cast Map - Cast instructions can have their source and destination values
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000108 // changed independently for each part. Because of this, our old naive
Chris Lattnerb1b42622002-07-17 17:11:33 +0000109 // 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.
112 //
113 std::set<ValueHandle> NewCasts;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000114};
Chris Lattner59cd9f12001-11-04 23:24:06 +0000115
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000116
Misha Brukmanf117cc92003-05-20 18:45:36 +0000117bool ExpressionConvertibleToType(Value *V, const Type *Ty, ValueTypeCache &Map,
Chris Lattner16125fb2003-04-24 18:25:27 +0000118 const TargetData &TD);
119Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC,
120 const TargetData &TD);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000121
Misha Brukmanf117cc92003-05-20 18:45:36 +0000122// ValueConvertibleToType - Return true if it is possible
123bool ValueConvertibleToType(Value *V, const Type *Ty,
Chris Lattner16125fb2003-04-24 18:25:27 +0000124 ValueTypeCache &ConvertedTypes,
125 const TargetData &TD);
Chris Lattner59cd9f12001-11-04 23:24:06 +0000126
Chris Lattner16125fb2003-04-24 18:25:27 +0000127void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC,
128 const TargetData &TD);
Chris Lattner59cd9f12001-11-04 23:24:06 +0000129
130
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000131// 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.
136//
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
141//
142const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
Chris Lattner697954c2002-01-20 22:54:45 +0000143 std::vector<Value*> &Offsets,
Chris Lattner16125fb2003-04-24 18:25:27 +0000144 const TargetData &TD, bool StopEarly = true);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000145
Brian Gaeked0fde302003-11-11 22:41:34 +0000146} // End llvm namespace
147
Chris Lattner59cd9f12001-11-04 23:24:06 +0000148#endif