blob: eb20544344b4b272bc4f445ad4aeafe7c81b3a71 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- TransformInternals.cpp - Implement shared functions for transforms -===//
John Criswellb576c942003-10-20 19:43:21 +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 file defines shared functions used by the different components of the
11// Transforms library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "TransformInternals.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000016#include "llvm/Type.h"
Chris Lattner89a1c802001-11-26 16:59:47 +000017#include "llvm/Analysis/Expressions.h"
Chris Lattner0ac54292002-04-09 19:08:28 +000018#include "llvm/Function.h"
Chris Lattner89a1c802001-11-26 16:59:47 +000019#include "llvm/iOther.h"
Chris Lattner559d5192004-01-09 05:53:38 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner8e2e5f72002-09-16 18:32:33 +000022static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
Chris Lattner16125fb2003-04-24 18:25:27 +000023 std::vector<Value*> &Indices,
24 const TargetData &TD) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000025 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
26 const StructLayout *SL = TD.getStructLayout(STy);
27
28 // This loop terminates always on a 0 <= i < MemberOffsets.size()
29 unsigned i;
30 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
31 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
32 break;
33
34 assert(Offset >= SL->MemberOffsets[i] &&
35 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
36
37 // Make sure to save the current index...
Chris Lattner559d5192004-01-09 05:53:38 +000038 // FIXME for PR82
Chris Lattner9e77f7e2002-03-21 06:27:20 +000039 Indices.push_back(ConstantUInt::get(Type::UByteTy, i));
40 Offset = SL->MemberOffsets[i];
41 return STy->getContainedType(i);
42}
43
44
Chris Lattnerc0b90e72001-11-08 20:19:56 +000045// getStructOffsetType - Return a vector of offsets that are to be used to index
46// into the specified struct type to get as close as possible to index as we
47// can. Note that it is possible that we cannot get exactly to Offset, in which
48// case we update offset to be the offset we actually obtained. The resultant
49// leaf type is returned.
50//
51// If StopEarly is set to true (the default), the first object with the
52// specified type is returned, even if it is a struct type itself. In this
53// case, this routine will not drill down to the leaf type. Set StopEarly to
54// false if you want a leaf
55//
Chris Lattner559d5192004-01-09 05:53:38 +000056const Type *llvm::getStructOffsetType(const Type *Ty, unsigned &Offset,
57 std::vector<Value*> &Indices,
58 const TargetData &TD, bool StopEarly) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000059 if (Offset == 0 && StopEarly && !Indices.empty())
Chris Lattnerc0b90e72001-11-08 20:19:56 +000060 return Ty; // Return the leaf type
Chris Lattnerc0b90e72001-11-08 20:19:56 +000061
Chris Lattner8e2e5f72002-09-16 18:32:33 +000062 uint64_t ThisOffset;
Chris Lattner89a1c802001-11-26 16:59:47 +000063 const Type *NextType;
64 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner94931012003-10-17 18:03:54 +000065 if (STy->getElementTypes().empty()) {
66 Offset = 0;
67 return STy;
68 }
69
Chris Lattner9e77f7e2002-03-21 06:27:20 +000070 ThisOffset = Offset;
Chris Lattner16125fb2003-04-24 18:25:27 +000071 NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
Chris Lattner7991c282001-12-14 16:38:59 +000072 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnere32487e2003-06-07 21:45:42 +000073 assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
74 "Offset not in composite!");
Chris Lattnerc0b90e72001-11-08 20:19:56 +000075
Chris Lattner89a1c802001-11-26 16:59:47 +000076 NextType = ATy->getElementType();
77 unsigned ChildSize = TD.getTypeSize(NextType);
Chris Lattner559d5192004-01-09 05:53:38 +000078 // FIXME for PR82
Chris Lattner106ff452002-09-11 01:21:29 +000079 Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize));
Chris Lattner89a1c802001-11-26 16:59:47 +000080 ThisOffset = (Offset/ChildSize)*ChildSize;
Chris Lattner7991c282001-12-14 16:38:59 +000081 } else {
Misha Brukmancf00c4a2003-10-10 17:57:28 +000082 Offset = 0; // Return the offset that we were able to achieve
Chris Lattner7991c282001-12-14 16:38:59 +000083 return Ty; // Return the leaf type
Chris Lattner89a1c802001-11-26 16:59:47 +000084 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +000085
Chris Lattner89a1c802001-11-26 16:59:47 +000086 unsigned SubOffs = Offset - ThisOffset;
Chris Lattner4736d062002-03-07 21:18:00 +000087 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
Chris Lattner16125fb2003-04-24 18:25:27 +000088 Indices, TD, StopEarly);
Chris Lattner89a1c802001-11-26 16:59:47 +000089 Offset = ThisOffset + SubOffs;
Chris Lattnerc0b90e72001-11-08 20:19:56 +000090 return LeafTy;
91}
Chris Lattner89a1c802001-11-26 16:59:47 +000092
Misha Brukmanf117cc92003-05-20 18:45:36 +000093// ConvertibleToGEP - This function returns true if the specified value V is
Chris Lattner89a1c802001-11-26 16:59:47 +000094// a valid index into a pointer of type Ty. If it is valid, Idx is filled in
95// with the values that would be appropriate to make this a getelementptr
96// instruction. The type returned is the root type that the GEP would point to
97//
Chris Lattner559d5192004-01-09 05:53:38 +000098const Type *llvm::ConvertibleToGEP(const Type *Ty, Value *OffsetVal,
99 std::vector<Value*> &Indices,
100 const TargetData &TD,
101 BasicBlock::iterator *BI) {
Chris Lattner7991c282001-12-14 16:38:59 +0000102 const CompositeType *CompTy = dyn_cast<CompositeType>(Ty);
Chris Lattner89a1c802001-11-26 16:59:47 +0000103 if (CompTy == 0) return 0;
104
105 // See if the cast is of an integer expression that is either a constant,
106 // or a value scaled by some amount with a possible offset.
107 //
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000108 ExprType Expr = ClassifyExpr(OffsetVal);
Chris Lattner89a1c802001-11-26 16:59:47 +0000109
Chris Lattner169bffe2002-04-16 22:10:36 +0000110 // Get the offset and scale values if they exists...
Chris Lattner99fb91c2002-03-21 03:04:38 +0000111 // A scale of zero with Expr.Var != 0 means a scale of 1.
112 //
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000113 int64_t Offset = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
114 int64_t Scale = Expr.Scale ? getConstantValue(Expr.Scale) : 0;
Chris Lattner89a1c802001-11-26 16:59:47 +0000115
Chris Lattner99fb91c2002-03-21 03:04:38 +0000116 if (Expr.Var && Scale == 0) Scale = 1; // Scale != 0 if Expr.Var != 0
117
Chris Lattner89a1c802001-11-26 16:59:47 +0000118 // Loop over the Scale and Offset values, filling in the Indices vector for
119 // our final getelementptr instruction.
120 //
121 const Type *NextTy = CompTy;
122 do {
123 if (!isa<CompositeType>(NextTy))
124 return 0; // Type must not be ready for processing...
125 CompTy = cast<CompositeType>(NextTy);
126
127 if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000128 // Step into the appropriate element of the structure...
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000129 uint64_t ActualOffset = (Offset < 0) ? 0 : (uint64_t)Offset;
Chris Lattner16125fb2003-04-24 18:25:27 +0000130 NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices, TD);
Chris Lattner89a1c802001-11-26 16:59:47 +0000131 Offset -= ActualOffset;
132 } else {
Chris Lattner7991c282001-12-14 16:38:59 +0000133 const Type *ElTy = cast<SequentialType>(CompTy)->getElementType();
Chris Lattnere5fa63a2003-01-23 02:39:10 +0000134 if (!ElTy->isSized() || (isa<PointerType>(CompTy) && !Indices.empty()))
Chris Lattner99fb91c2002-03-21 03:04:38 +0000135 return 0; // Type is unreasonable... escape!
Chris Lattner89a1c802001-11-26 16:59:47 +0000136 unsigned ElSize = TD.getTypeSize(ElTy);
Chris Lattner8a334a42003-06-23 17:36:49 +0000137 if (ElSize == 0) return 0; // Avoid division by zero...
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000138 int64_t ElSizeS = ElSize;
Chris Lattner89a1c802001-11-26 16:59:47 +0000139
140 // See if the user is indexing into a different cell of this array...
Chris Lattner99fb91c2002-03-21 03:04:38 +0000141 if (Scale && (Scale >= ElSizeS || -Scale >= ElSizeS)) {
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000142 // A scale n*ElSize might occur if we are not stepping through
143 // array by one. In this case, we will have to insert math to munge
144 // the index.
145 //
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000146 int64_t ScaleAmt = Scale/ElSizeS;
Chris Lattner99fb91c2002-03-21 03:04:38 +0000147 if (Scale-ScaleAmt*ElSizeS)
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000148 return 0; // Didn't scale by a multiple of element size, bail out
Chris Lattner7991c282001-12-14 16:38:59 +0000149 Scale = 0; // Scale is consumed
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000150
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000151 int64_t Index = Offset/ElSize; // is zero unless Offset > ElSize
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000152 Offset -= Index*ElSize; // Consume part of the offset
153
154 if (BI) { // Generate code?
Chris Lattner7e708292002-06-25 16:13:24 +0000155 BasicBlock *BB = (*BI)->getParent();
Chris Lattner559d5192004-01-09 05:53:38 +0000156 if (Expr.Var->getType() != Type::LongTy) // FIXME for PR82
157 Expr.Var = new CastInst(Expr.Var, Type::LongTy, // FIXME for PR82
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000158 Expr.Var->getName()+"-idxcast", *BI);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000159
Chris Lattner7c3f4152001-12-07 04:26:02 +0000160 if (ScaleAmt && ScaleAmt != 1) {
161 // If we have to scale up our index, do so now
Chris Lattner559d5192004-01-09 05:53:38 +0000162 // FIXME for PR82
Chris Lattnerb6984552002-10-02 18:53:14 +0000163 Value *ScaleAmtVal = ConstantSInt::get(Type::LongTy, ScaleAmt);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000164 Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var,
165 ScaleAmtVal,
166 Expr.Var->getName()+"-scale",*BI);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000167 }
168
169 if (Index) { // Add an offset to the index
Chris Lattner559d5192004-01-09 05:53:38 +0000170 // FIXME for PR82
Chris Lattnerb6984552002-10-02 18:53:14 +0000171 Value *IndexAmt = ConstantSInt::get(Type::LongTy, Index);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000172 Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var,
173 IndexAmt,
174 Expr.Var->getName()+"-offset",
175 *BI);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000176 }
177 }
178
179 Indices.push_back(Expr.Var);
Chris Lattner99fb91c2002-03-21 03:04:38 +0000180 Expr.Var = 0;
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000181 } else if (Offset >= (int64_t)ElSize || -Offset >= (int64_t)ElSize) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000182 // Calculate the index that we are entering into the array cell with
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000183 uint64_t Index = Offset/ElSize;
Chris Lattner559d5192004-01-09 05:53:38 +0000184 // FIXME for PR82
Chris Lattner106ff452002-09-11 01:21:29 +0000185 Indices.push_back(ConstantSInt::get(Type::LongTy, Index));
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000186 Offset -= (int64_t)(Index*ElSize); // Consume part of the offset
Chris Lattner89a1c802001-11-26 16:59:47 +0000187
Chris Lattner754cf412002-03-14 16:37:04 +0000188 } else if (isa<ArrayType>(CompTy) || Indices.empty()) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000189 // Must be indexing a small amount into the first cell of the array
190 // Just index into element zero of the array here.
191 //
Chris Lattner559d5192004-01-09 05:53:38 +0000192 // FIXME for PR82
Chris Lattner106ff452002-09-11 01:21:29 +0000193 Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
Chris Lattner7991c282001-12-14 16:38:59 +0000194 } else {
195 return 0; // Hrm. wierd, can't handle this case. Bail
Chris Lattner89a1c802001-11-26 16:59:47 +0000196 }
197 NextTy = ElTy;
198 }
199 } while (Offset || Scale); // Go until we're done!
200
201 return NextTy;
202}
Brian Gaeked0fde302003-11-11 22:41:34 +0000203