blob: 5117ce8dd56d73a5122d66b35b5abf307cf4b486 [file] [log] [blame]
Chris Lattner59cd9f12001-11-04 23:24:06 +00001//===-- TransformInternals.cpp - Implement shared functions for transforms --=//
2//
3// This file defines shared functions used by the different components of the
4// Transforms library.
5//
6//===----------------------------------------------------------------------===//
7
8#include "TransformInternals.h"
9#include "llvm/Method.h"
10#include "llvm/Type.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000011#include "llvm/ConstantVals.h"
Chris Lattner89a1c802001-11-26 16:59:47 +000012#include "llvm/Analysis/Expressions.h"
13#include "llvm/iOther.h"
Chris Lattner7991c282001-12-14 16:38:59 +000014#include <algorithm>
Chris Lattner59cd9f12001-11-04 23:24:06 +000015
16// TargetData Hack: Eventually we will have annotations given to us by the
17// backend so that we know stuff about type size and alignments. For now
18// though, just use this, because it happens to match the model that GCC uses.
19//
20const TargetData TD("LevelRaise: Should be GCC though!");
21
Chris Lattner59cd9f12001-11-04 23:24:06 +000022// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
23// with a value, then remove and delete the original instruction.
24//
25void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
26 BasicBlock::iterator &BI, Value *V) {
27 Instruction *I = *BI;
28 // Replaces all of the uses of the instruction with uses of the value
29 I->replaceAllUsesWith(V);
30
31 // Remove the unneccesary instruction now...
32 BIL.remove(BI);
33
34 // Make sure to propogate a name if there is one already...
35 if (I->hasName() && !V->hasName())
36 V->setName(I->getName(), BIL.getParent()->getSymbolTable());
37
38 // Remove the dead instruction now...
39 delete I;
40}
41
42
43// ReplaceInstWithInst - Replace the instruction specified by BI with the
44// instruction specified by I. The original instruction is deleted and BI is
45// updated to point to the new instruction.
46//
47void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
48 BasicBlock::iterator &BI, Instruction *I) {
49 assert(I->getParent() == 0 &&
50 "ReplaceInstWithInst: Instruction already inserted into basic block!");
51
52 // Insert the new instruction into the basic block...
Chris Lattnerc4db7542002-03-11 22:21:04 +000053 BI = BIL.insert(BI, I)+1; // Increment BI to point to instruction to delete
Chris Lattner59cd9f12001-11-04 23:24:06 +000054
55 // Replace all uses of the old instruction, and delete it.
56 ReplaceInstWithValue(BIL, BI, I);
57
Chris Lattnerc4db7542002-03-11 22:21:04 +000058 // Move BI back to point to the newly inserted instruction
Chris Lattner59cd9f12001-11-04 23:24:06 +000059 --BI;
60}
61
Chris Lattner7991c282001-12-14 16:38:59 +000062void ReplaceInstWithInst(Instruction *From, Instruction *To) {
63 BasicBlock *BB = From->getParent();
64 BasicBlock::InstListType &BIL = BB->getInstList();
65 BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), From);
66 assert(BI != BIL.end() && "Inst not in it's parents BB!");
67 ReplaceInstWithInst(BIL, BI, To);
68}
69
70
Chris Lattner59cd9f12001-11-04 23:24:06 +000071
Chris Lattnerc0b90e72001-11-08 20:19:56 +000072// getStructOffsetType - Return a vector of offsets that are to be used to index
73// into the specified struct type to get as close as possible to index as we
74// can. Note that it is possible that we cannot get exactly to Offset, in which
75// case we update offset to be the offset we actually obtained. The resultant
76// leaf type is returned.
77//
78// If StopEarly is set to true (the default), the first object with the
79// specified type is returned, even if it is a struct type itself. In this
80// case, this routine will not drill down to the leaf type. Set StopEarly to
81// false if you want a leaf
82//
83const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
Chris Lattner697954c2002-01-20 22:54:45 +000084 std::vector<Value*> &Offsets,
Chris Lattnerc0b90e72001-11-08 20:19:56 +000085 bool StopEarly = true) {
Chris Lattner7991c282001-12-14 16:38:59 +000086 if (Offset == 0 && StopEarly && !Offsets.empty())
Chris Lattnerc0b90e72001-11-08 20:19:56 +000087 return Ty; // Return the leaf type
Chris Lattnerc0b90e72001-11-08 20:19:56 +000088
Chris Lattner89a1c802001-11-26 16:59:47 +000089 unsigned ThisOffset;
90 const Type *NextType;
91 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner7991c282001-12-14 16:38:59 +000092 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
Chris Lattner89a1c802001-11-26 16:59:47 +000093 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerc0b90e72001-11-08 20:19:56 +000094
Chris Lattner89a1c802001-11-26 16:59:47 +000095 // This loop terminates always on a 0 <= i < MemberOffsets.size()
96 unsigned i;
97 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
98 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
99 break;
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000100
Chris Lattner89a1c802001-11-26 16:59:47 +0000101 assert(Offset >= SL->MemberOffsets[i] &&
102 (i == SL->MemberOffsets.size()-1 || Offset <SL->MemberOffsets[i+1]));
103
104 // Make sure to save the current index...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000105 Offsets.push_back(ConstantUInt::get(Type::UByteTy, i));
Chris Lattner89a1c802001-11-26 16:59:47 +0000106 ThisOffset = SL->MemberOffsets[i];
107 NextType = STy->getElementTypes()[i];
Chris Lattner7991c282001-12-14 16:38:59 +0000108 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
109 assert(Offset < TD.getTypeSize(ATy) && "Offset not in composite!");
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000110
Chris Lattner89a1c802001-11-26 16:59:47 +0000111 NextType = ATy->getElementType();
112 unsigned ChildSize = TD.getTypeSize(NextType);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000113 Offsets.push_back(ConstantUInt::get(Type::UIntTy, Offset/ChildSize));
Chris Lattner89a1c802001-11-26 16:59:47 +0000114 ThisOffset = (Offset/ChildSize)*ChildSize;
Chris Lattner7991c282001-12-14 16:38:59 +0000115 } else {
116 Offset = 0; // Return the offset that we were able to acheive
117 return Ty; // Return the leaf type
Chris Lattner89a1c802001-11-26 16:59:47 +0000118 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000119
Chris Lattner89a1c802001-11-26 16:59:47 +0000120 unsigned SubOffs = Offset - ThisOffset;
Chris Lattner4736d062002-03-07 21:18:00 +0000121 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
122 Offsets, StopEarly);
Chris Lattner89a1c802001-11-26 16:59:47 +0000123 Offset = ThisOffset + SubOffs;
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000124 return LeafTy;
125}
Chris Lattner89a1c802001-11-26 16:59:47 +0000126
127// ConvertableToGEP - This function returns true if the specified value V is
128// a valid index into a pointer of type Ty. If it is valid, Idx is filled in
129// with the values that would be appropriate to make this a getelementptr
130// instruction. The type returned is the root type that the GEP would point to
131//
132const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
Chris Lattner697954c2002-01-20 22:54:45 +0000133 std::vector<Value*> &Indices,
Chris Lattner89a1c802001-11-26 16:59:47 +0000134 BasicBlock::iterator *BI = 0) {
Chris Lattner7991c282001-12-14 16:38:59 +0000135 const CompositeType *CompTy = dyn_cast<CompositeType>(Ty);
Chris Lattner89a1c802001-11-26 16:59:47 +0000136 if (CompTy == 0) return 0;
137
138 // See if the cast is of an integer expression that is either a constant,
139 // or a value scaled by some amount with a possible offset.
140 //
141 analysis::ExprType Expr = analysis::ClassifyExpression(OffsetVal);
142
Chris Lattner89a1c802001-11-26 16:59:47 +0000143 // Get the offset and scale now...
144 unsigned Offset = 0, Scale = Expr.Var != 0;
145
146 // Get the offset value if it exists...
147 if (Expr.Offset) {
148 int Val = getConstantValue(Expr.Offset);
149 if (Val < 0) return false; // Don't mess with negative offsets
150 Offset = (unsigned)Val;
151 }
152
153 // Get the scale value if it exists...
154 if (Expr.Scale) {
155 int Val = getConstantValue(Expr.Scale);
156 if (Val < 0) return false; // Don't mess with negative scales
157 Scale = (unsigned)Val;
Chris Lattner868c2d32002-02-14 22:21:40 +0000158 if (Scale == 1) Scale = 0; // No interesting scale if *1
Chris Lattner89a1c802001-11-26 16:59:47 +0000159 }
160
Chris Lattner89a1c802001-11-26 16:59:47 +0000161 // Loop over the Scale and Offset values, filling in the Indices vector for
162 // our final getelementptr instruction.
163 //
164 const Type *NextTy = CompTy;
165 do {
166 if (!isa<CompositeType>(NextTy))
167 return 0; // Type must not be ready for processing...
168 CompTy = cast<CompositeType>(NextTy);
169
170 if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000171 unsigned ActualOffset = Offset;
172 NextTy = getStructOffsetType(StructTy, ActualOffset, Indices);
Chris Lattner7991c282001-12-14 16:38:59 +0000173 if (StructTy == NextTy && ActualOffset == 0) return 0; // No progress. :(
Chris Lattner89a1c802001-11-26 16:59:47 +0000174 Offset -= ActualOffset;
175 } else {
Chris Lattner7991c282001-12-14 16:38:59 +0000176 const Type *ElTy = cast<SequentialType>(CompTy)->getElementType();
177 if (!ElTy->isSized()) return 0; // Type is unreasonable... escape!
Chris Lattner89a1c802001-11-26 16:59:47 +0000178 unsigned ElSize = TD.getTypeSize(ElTy);
179
180 // See if the user is indexing into a different cell of this array...
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000181 if (Scale && Scale >= ElSize) {
182 // A scale n*ElSize might occur if we are not stepping through
183 // array by one. In this case, we will have to insert math to munge
184 // the index.
185 //
186 unsigned ScaleAmt = Scale/ElSize;
187 if (Scale-ScaleAmt*ElSize)
188 return 0; // Didn't scale by a multiple of element size, bail out
Chris Lattner7991c282001-12-14 16:38:59 +0000189 Scale = 0; // Scale is consumed
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000190
191 unsigned Index = Offset/ElSize; // is zero unless Offset > ElSize
192 Offset -= Index*ElSize; // Consume part of the offset
193
194 if (BI) { // Generate code?
195 BasicBlock *BB = (**BI)->getParent();
196 if (Expr.Var->getType() != Type::UIntTy) {
197 CastInst *IdxCast = new CastInst(Expr.Var, Type::UIntTy);
198 if (Expr.Var->hasName())
199 IdxCast->setName(Expr.Var->getName()+"-idxcast");
200 *BI = BB->getInstList().insert(*BI, IdxCast)+1;
201 Expr.Var = IdxCast;
202 }
203
Chris Lattner7c3f4152001-12-07 04:26:02 +0000204 if (ScaleAmt && ScaleAmt != 1) {
205 // If we have to scale up our index, do so now
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000206 Value *ScaleAmtVal = ConstantUInt::get(Type::UIntTy, ScaleAmt);
207 Instruction *Scaler = BinaryOperator::create(Instruction::Mul,
208 Expr.Var,ScaleAmtVal);
209 if (Expr.Var->hasName())
210 Scaler->setName(Expr.Var->getName()+"-scale");
211
212 *BI = BB->getInstList().insert(*BI, Scaler)+1;
213 Expr.Var = Scaler;
214 }
215
216 if (Index) { // Add an offset to the index
217 Value *IndexAmt = ConstantUInt::get(Type::UIntTy, Index);
218 Instruction *Offseter = BinaryOperator::create(Instruction::Add,
219 Expr.Var, IndexAmt);
220 if (Expr.Var->hasName())
221 Offseter->setName(Expr.Var->getName()+"-offset");
222 *BI = BB->getInstList().insert(*BI, Offseter)+1;
223 Expr.Var = Offseter;
224 }
225 }
226
227 Indices.push_back(Expr.Var);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000228 } else if (Offset >= ElSize) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000229 // Calculate the index that we are entering into the array cell with
230 unsigned Index = Offset/ElSize;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000231 Indices.push_back(ConstantUInt::get(Type::UIntTy, Index));
Chris Lattner89a1c802001-11-26 16:59:47 +0000232 Offset -= Index*ElSize; // Consume part of the offset
233
Chris Lattner754cf412002-03-14 16:37:04 +0000234 } else if (isa<ArrayType>(CompTy) || Indices.empty()) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000235 // Must be indexing a small amount into the first cell of the array
236 // Just index into element zero of the array here.
237 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000238 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
Chris Lattner7991c282001-12-14 16:38:59 +0000239 } else {
240 return 0; // Hrm. wierd, can't handle this case. Bail
Chris Lattner89a1c802001-11-26 16:59:47 +0000241 }
242 NextTy = ElTy;
243 }
244 } while (Offset || Scale); // Go until we're done!
245
246 return NextTy;
247}