blob: 820dd3878e0c49d31fcb66ad9f415a6f76275a6c [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- TransformInternals.cpp - Implement shared functions for transforms -===//
Chris Lattner59cd9f12001-11-04 23:24:06 +00002//
3// This file defines shared functions used by the different components of the
4// Transforms library.
5//
6//===----------------------------------------------------------------------===//
7
8#include "TransformInternals.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +00009#include "llvm/Type.h"
Chris Lattner89a1c802001-11-26 16:59:47 +000010#include "llvm/Analysis/Expressions.h"
Chris Lattner0ac54292002-04-09 19:08:28 +000011#include "llvm/Function.h"
Chris Lattner89a1c802001-11-26 16:59:47 +000012#include "llvm/iOther.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000013
Chris Lattner8e2e5f72002-09-16 18:32:33 +000014static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
Chris Lattner16125fb2003-04-24 18:25:27 +000015 std::vector<Value*> &Indices,
16 const TargetData &TD) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000017 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
18 const StructLayout *SL = TD.getStructLayout(STy);
19
20 // This loop terminates always on a 0 <= i < MemberOffsets.size()
21 unsigned i;
22 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
23 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
24 break;
25
26 assert(Offset >= SL->MemberOffsets[i] &&
27 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
28
29 // Make sure to save the current index...
30 Indices.push_back(ConstantUInt::get(Type::UByteTy, i));
31 Offset = SL->MemberOffsets[i];
32 return STy->getContainedType(i);
33}
34
35
Chris Lattnerc0b90e72001-11-08 20:19:56 +000036// getStructOffsetType - Return a vector of offsets that are to be used to index
37// into the specified struct type to get as close as possible to index as we
38// can. Note that it is possible that we cannot get exactly to Offset, in which
39// case we update offset to be the offset we actually obtained. The resultant
40// leaf type is returned.
41//
42// If StopEarly is set to true (the default), the first object with the
43// specified type is returned, even if it is a struct type itself. In this
44// case, this routine will not drill down to the leaf type. Set StopEarly to
45// false if you want a leaf
46//
47const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
Chris Lattner9e77f7e2002-03-21 06:27:20 +000048 std::vector<Value*> &Indices,
Chris Lattner16125fb2003-04-24 18:25:27 +000049 const TargetData &TD, bool StopEarly) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000050 if (Offset == 0 && StopEarly && !Indices.empty())
Chris Lattnerc0b90e72001-11-08 20:19:56 +000051 return Ty; // Return the leaf type
Chris Lattnerc0b90e72001-11-08 20:19:56 +000052
Chris Lattner8e2e5f72002-09-16 18:32:33 +000053 uint64_t ThisOffset;
Chris Lattner89a1c802001-11-26 16:59:47 +000054 const Type *NextType;
55 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner94931012003-10-17 18:03:54 +000056 if (STy->getElementTypes().empty()) {
57 Offset = 0;
58 return STy;
59 }
60
Chris Lattner9e77f7e2002-03-21 06:27:20 +000061 ThisOffset = Offset;
Chris Lattner16125fb2003-04-24 18:25:27 +000062 NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
Chris Lattner7991c282001-12-14 16:38:59 +000063 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnere32487e2003-06-07 21:45:42 +000064 assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
65 "Offset not in composite!");
Chris Lattnerc0b90e72001-11-08 20:19:56 +000066
Chris Lattner89a1c802001-11-26 16:59:47 +000067 NextType = ATy->getElementType();
68 unsigned ChildSize = TD.getTypeSize(NextType);
Chris Lattner106ff452002-09-11 01:21:29 +000069 Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize));
Chris Lattner89a1c802001-11-26 16:59:47 +000070 ThisOffset = (Offset/ChildSize)*ChildSize;
Chris Lattner7991c282001-12-14 16:38:59 +000071 } else {
Misha Brukmancf00c4a2003-10-10 17:57:28 +000072 Offset = 0; // Return the offset that we were able to achieve
Chris Lattner7991c282001-12-14 16:38:59 +000073 return Ty; // Return the leaf type
Chris Lattner89a1c802001-11-26 16:59:47 +000074 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +000075
Chris Lattner89a1c802001-11-26 16:59:47 +000076 unsigned SubOffs = Offset - ThisOffset;
Chris Lattner4736d062002-03-07 21:18:00 +000077 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
Chris Lattner16125fb2003-04-24 18:25:27 +000078 Indices, TD, StopEarly);
Chris Lattner89a1c802001-11-26 16:59:47 +000079 Offset = ThisOffset + SubOffs;
Chris Lattnerc0b90e72001-11-08 20:19:56 +000080 return LeafTy;
81}
Chris Lattner89a1c802001-11-26 16:59:47 +000082
Misha Brukmanf117cc92003-05-20 18:45:36 +000083// ConvertibleToGEP - This function returns true if the specified value V is
Chris Lattner89a1c802001-11-26 16:59:47 +000084// a valid index into a pointer of type Ty. If it is valid, Idx is filled in
85// with the values that would be appropriate to make this a getelementptr
86// instruction. The type returned is the root type that the GEP would point to
87//
Misha Brukmanf117cc92003-05-20 18:45:36 +000088const Type *ConvertibleToGEP(const Type *Ty, Value *OffsetVal,
Chris Lattner697954c2002-01-20 22:54:45 +000089 std::vector<Value*> &Indices,
Chris Lattner16125fb2003-04-24 18:25:27 +000090 const TargetData &TD,
Chris Lattner0c0edf82002-07-25 06:17:51 +000091 BasicBlock::iterator *BI) {
Chris Lattner7991c282001-12-14 16:38:59 +000092 const CompositeType *CompTy = dyn_cast<CompositeType>(Ty);
Chris Lattner89a1c802001-11-26 16:59:47 +000093 if (CompTy == 0) return 0;
94
95 // See if the cast is of an integer expression that is either a constant,
96 // or a value scaled by some amount with a possible offset.
97 //
Chris Lattnerc74cb862002-08-30 22:53:53 +000098 ExprType Expr = ClassifyExpression(OffsetVal);
Chris Lattner89a1c802001-11-26 16:59:47 +000099
Chris Lattner169bffe2002-04-16 22:10:36 +0000100 // Get the offset and scale values if they exists...
Chris Lattner99fb91c2002-03-21 03:04:38 +0000101 // A scale of zero with Expr.Var != 0 means a scale of 1.
102 //
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000103 int64_t Offset = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
104 int64_t Scale = Expr.Scale ? getConstantValue(Expr.Scale) : 0;
Chris Lattner89a1c802001-11-26 16:59:47 +0000105
Chris Lattner99fb91c2002-03-21 03:04:38 +0000106 if (Expr.Var && Scale == 0) Scale = 1; // Scale != 0 if Expr.Var != 0
107
Chris Lattner89a1c802001-11-26 16:59:47 +0000108 // Loop over the Scale and Offset values, filling in the Indices vector for
109 // our final getelementptr instruction.
110 //
111 const Type *NextTy = CompTy;
112 do {
113 if (!isa<CompositeType>(NextTy))
114 return 0; // Type must not be ready for processing...
115 CompTy = cast<CompositeType>(NextTy);
116
117 if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000118 // Step into the appropriate element of the structure...
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000119 uint64_t ActualOffset = (Offset < 0) ? 0 : (uint64_t)Offset;
Chris Lattner16125fb2003-04-24 18:25:27 +0000120 NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices, TD);
Chris Lattner89a1c802001-11-26 16:59:47 +0000121 Offset -= ActualOffset;
122 } else {
Chris Lattner7991c282001-12-14 16:38:59 +0000123 const Type *ElTy = cast<SequentialType>(CompTy)->getElementType();
Chris Lattnere5fa63a2003-01-23 02:39:10 +0000124 if (!ElTy->isSized() || (isa<PointerType>(CompTy) && !Indices.empty()))
Chris Lattner99fb91c2002-03-21 03:04:38 +0000125 return 0; // Type is unreasonable... escape!
Chris Lattner89a1c802001-11-26 16:59:47 +0000126 unsigned ElSize = TD.getTypeSize(ElTy);
Chris Lattner8a334a42003-06-23 17:36:49 +0000127 if (ElSize == 0) return 0; // Avoid division by zero...
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000128 int64_t ElSizeS = ElSize;
Chris Lattner89a1c802001-11-26 16:59:47 +0000129
130 // See if the user is indexing into a different cell of this array...
Chris Lattner99fb91c2002-03-21 03:04:38 +0000131 if (Scale && (Scale >= ElSizeS || -Scale >= ElSizeS)) {
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000132 // A scale n*ElSize might occur if we are not stepping through
133 // array by one. In this case, we will have to insert math to munge
134 // the index.
135 //
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000136 int64_t ScaleAmt = Scale/ElSizeS;
Chris Lattner99fb91c2002-03-21 03:04:38 +0000137 if (Scale-ScaleAmt*ElSizeS)
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000138 return 0; // Didn't scale by a multiple of element size, bail out
Chris Lattner7991c282001-12-14 16:38:59 +0000139 Scale = 0; // Scale is consumed
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000140
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000141 int64_t Index = Offset/ElSize; // is zero unless Offset > ElSize
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000142 Offset -= Index*ElSize; // Consume part of the offset
143
144 if (BI) { // Generate code?
Chris Lattner7e708292002-06-25 16:13:24 +0000145 BasicBlock *BB = (*BI)->getParent();
Chris Lattner106ff452002-09-11 01:21:29 +0000146 if (Expr.Var->getType() != Type::LongTy)
147 Expr.Var = new CastInst(Expr.Var, Type::LongTy,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000148 Expr.Var->getName()+"-idxcast", *BI);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000149
Chris Lattner7c3f4152001-12-07 04:26:02 +0000150 if (ScaleAmt && ScaleAmt != 1) {
151 // If we have to scale up our index, do so now
Chris Lattnerb6984552002-10-02 18:53:14 +0000152 Value *ScaleAmtVal = ConstantSInt::get(Type::LongTy, ScaleAmt);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000153 Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var,
154 ScaleAmtVal,
155 Expr.Var->getName()+"-scale",*BI);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000156 }
157
158 if (Index) { // Add an offset to the index
Chris Lattnerb6984552002-10-02 18:53:14 +0000159 Value *IndexAmt = ConstantSInt::get(Type::LongTy, Index);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000160 Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var,
161 IndexAmt,
162 Expr.Var->getName()+"-offset",
163 *BI);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000164 }
165 }
166
167 Indices.push_back(Expr.Var);
Chris Lattner99fb91c2002-03-21 03:04:38 +0000168 Expr.Var = 0;
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000169 } else if (Offset >= (int64_t)ElSize || -Offset >= (int64_t)ElSize) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000170 // Calculate the index that we are entering into the array cell with
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000171 uint64_t Index = Offset/ElSize;
Chris Lattner106ff452002-09-11 01:21:29 +0000172 Indices.push_back(ConstantSInt::get(Type::LongTy, Index));
Chris Lattner8e2e5f72002-09-16 18:32:33 +0000173 Offset -= (int64_t)(Index*ElSize); // Consume part of the offset
Chris Lattner89a1c802001-11-26 16:59:47 +0000174
Chris Lattner754cf412002-03-14 16:37:04 +0000175 } else if (isa<ArrayType>(CompTy) || Indices.empty()) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000176 // Must be indexing a small amount into the first cell of the array
177 // Just index into element zero of the array here.
178 //
Chris Lattner106ff452002-09-11 01:21:29 +0000179 Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
Chris Lattner7991c282001-12-14 16:38:59 +0000180 } else {
181 return 0; // Hrm. wierd, can't handle this case. Bail
Chris Lattner89a1c802001-11-26 16:59:47 +0000182 }
183 NextTy = ElTy;
184 }
185 } while (Offset || Scale); // Go until we're done!
186
187 return NextTy;
188}