blob: f170592ece3d3e8f212b599c3459767b143d6381 [file] [log] [blame]
Nate Begemanb18121e2004-10-18 21:08:22 +00001//===- LoopStrengthReduce.cpp - Strength Reduce GEPs in Loops -------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Nate Begemanb18121e2004-10-18 21:08:22 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Nate Begemanb18121e2004-10-18 21:08:22 +00008//===----------------------------------------------------------------------===//
9//
10// This pass performs a strength reduction on array references inside loops that
11// have as one or more of their components the loop induction variable. This is
12// accomplished by creating a new Value to hold the initial value of the array
13// access for the first iteration, and then creating a new GEP instruction in
14// the loop to increment the value by the appropriate amount.
15//
Nate Begemanb18121e2004-10-18 21:08:22 +000016//===----------------------------------------------------------------------===//
17
Chris Lattnerbb78c972005-08-03 23:30:08 +000018#define DEBUG_TYPE "loop-reduce"
Nate Begemanb18121e2004-10-18 21:08:22 +000019#include "llvm/Transforms/Scalar.h"
20#include "llvm/Constants.h"
21#include "llvm/Instructions.h"
22#include "llvm/Type.h"
Jeff Cohena2c59b72005-03-04 04:04:26 +000023#include "llvm/DerivedTypes.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000024#include "llvm/Analysis/Dominators.h"
25#include "llvm/Analysis/LoopInfo.h"
Nate Begemane68bcd12005-07-30 00:15:07 +000026#include "llvm/Analysis/ScalarEvolutionExpander.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000027#include "llvm/Support/CFG.h"
Nate Begemane68bcd12005-07-30 00:15:07 +000028#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner4fec86d2005-08-12 22:06:11 +000029#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000030#include "llvm/Transforms/Utils/Local.h"
Jeff Cohena2c59b72005-03-04 04:04:26 +000031#include "llvm/Target/TargetData.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000032#include "llvm/ADT/Statistic.h"
Nate Begemane68bcd12005-07-30 00:15:07 +000033#include "llvm/Support/Debug.h"
Jeff Cohenc5009912005-07-30 18:22:27 +000034#include <algorithm>
Nate Begemanb18121e2004-10-18 21:08:22 +000035#include <set>
36using namespace llvm;
37
38namespace {
39 Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced");
Chris Lattner45f8b6e2005-08-04 22:34:05 +000040 Statistic<> NumInserted("loop-reduce", "Number of PHIs inserted");
Chris Lattneredff91a2005-08-10 00:45:21 +000041 Statistic<> NumVariable("loop-reduce","Number of PHIs with variable strides");
Nate Begemanb18121e2004-10-18 21:08:22 +000042
Chris Lattner430d0022005-08-03 22:21:05 +000043 /// IVStrideUse - Keep track of one use of a strided induction variable, where
44 /// the stride is stored externally. The Offset member keeps track of the
45 /// offset from the IV, User is the actual user of the operand, and 'Operand'
46 /// is the operand # of the User that is the use.
47 struct IVStrideUse {
48 SCEVHandle Offset;
49 Instruction *User;
50 Value *OperandValToReplace;
Chris Lattner9bfa6f82005-08-08 05:28:22 +000051
52 // isUseOfPostIncrementedValue - True if this should use the
53 // post-incremented version of this IV, not the preincremented version.
54 // This can only be set in special cases, such as the terminating setcc
Chris Lattnera6764832005-09-12 06:04:47 +000055 // instruction for a loop or uses dominated by the loop.
Chris Lattner9bfa6f82005-08-08 05:28:22 +000056 bool isUseOfPostIncrementedValue;
Chris Lattner430d0022005-08-03 22:21:05 +000057
58 IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O)
Chris Lattner9bfa6f82005-08-08 05:28:22 +000059 : Offset(Offs), User(U), OperandValToReplace(O),
60 isUseOfPostIncrementedValue(false) {}
Chris Lattner430d0022005-08-03 22:21:05 +000061 };
62
63 /// IVUsersOfOneStride - This structure keeps track of all instructions that
64 /// have an operand that is based on the trip count multiplied by some stride.
65 /// The stride for all of these users is common and kept external to this
66 /// structure.
67 struct IVUsersOfOneStride {
Nate Begemane68bcd12005-07-30 00:15:07 +000068 /// Users - Keep track of all of the users of this stride as well as the
Chris Lattner430d0022005-08-03 22:21:05 +000069 /// initial value and the operand that uses the IV.
70 std::vector<IVStrideUse> Users;
71
72 void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) {
73 Users.push_back(IVStrideUse(Offset, User, Operand));
Nate Begemane68bcd12005-07-30 00:15:07 +000074 }
75 };
76
77
Nate Begemanb18121e2004-10-18 21:08:22 +000078 class LoopStrengthReduce : public FunctionPass {
79 LoopInfo *LI;
80 DominatorSet *DS;
Nate Begemane68bcd12005-07-30 00:15:07 +000081 ScalarEvolution *SE;
82 const TargetData *TD;
83 const Type *UIntPtrTy;
Nate Begemanb18121e2004-10-18 21:08:22 +000084 bool Changed;
Chris Lattner75a44e12005-08-02 02:52:02 +000085
86 /// MaxTargetAMSize - This is the maximum power-of-two scale value that the
87 /// target can handle for free with its addressing modes.
Jeff Cohena2c59b72005-03-04 04:04:26 +000088 unsigned MaxTargetAMSize;
Nate Begemane68bcd12005-07-30 00:15:07 +000089
90 /// IVUsesByStride - Keep track of all uses of induction variables that we
91 /// are interested in. The key of the map is the stride of the access.
Chris Lattneredff91a2005-08-10 00:45:21 +000092 std::map<SCEVHandle, IVUsersOfOneStride> IVUsesByStride;
Nate Begemane68bcd12005-07-30 00:15:07 +000093
Chris Lattner4ea0a3e2005-10-09 06:20:55 +000094 /// StrideOrder - An ordering of the keys in IVUsesByStride that is stable:
95 /// We use this to iterate over the IVUsesByStride collection without being
96 /// dependent on random ordering of pointers in the process.
97 std::vector<SCEVHandle> StrideOrder;
98
Chris Lattner6f286b72005-08-04 01:19:13 +000099 /// CastedValues - As we need to cast values to uintptr_t, this keeps track
100 /// of the casted version of each value. This is accessed by
101 /// getCastedVersionOf.
102 std::map<Value*, Value*> CastedPointers;
Nate Begemane68bcd12005-07-30 00:15:07 +0000103
104 /// DeadInsts - Keep track of instructions we may have made dead, so that
105 /// we can remove them after we are done working.
106 std::set<Instruction*> DeadInsts;
Nate Begemanb18121e2004-10-18 21:08:22 +0000107 public:
Jeff Cohena2c59b72005-03-04 04:04:26 +0000108 LoopStrengthReduce(unsigned MTAMS = 1)
109 : MaxTargetAMSize(MTAMS) {
110 }
111
Nate Begemanb18121e2004-10-18 21:08:22 +0000112 virtual bool runOnFunction(Function &) {
113 LI = &getAnalysis<LoopInfo>();
114 DS = &getAnalysis<DominatorSet>();
Nate Begemane68bcd12005-07-30 00:15:07 +0000115 SE = &getAnalysis<ScalarEvolution>();
116 TD = &getAnalysis<TargetData>();
117 UIntPtrTy = TD->getIntPtrType();
Nate Begemanb18121e2004-10-18 21:08:22 +0000118 Changed = false;
119
120 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
121 runOnLoop(*I);
Chris Lattner6f286b72005-08-04 01:19:13 +0000122
Nate Begemanb18121e2004-10-18 21:08:22 +0000123 return Changed;
124 }
125
126 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner2bf7cb52005-08-17 06:35:16 +0000127 // We split critical edges, so we change the CFG. However, we do update
128 // many analyses if they are around.
129 AU.addPreservedID(LoopSimplifyID);
130 AU.addPreserved<LoopInfo>();
131 AU.addPreserved<DominatorSet>();
132 AU.addPreserved<ImmediateDominators>();
133 AU.addPreserved<DominanceFrontier>();
134 AU.addPreserved<DominatorTree>();
135
Jeff Cohen39751c32005-02-27 19:37:07 +0000136 AU.addRequiredID(LoopSimplifyID);
Nate Begemanb18121e2004-10-18 21:08:22 +0000137 AU.addRequired<LoopInfo>();
138 AU.addRequired<DominatorSet>();
Jeff Cohena2c59b72005-03-04 04:04:26 +0000139 AU.addRequired<TargetData>();
Nate Begemane68bcd12005-07-30 00:15:07 +0000140 AU.addRequired<ScalarEvolution>();
Nate Begemanb18121e2004-10-18 21:08:22 +0000141 }
Chris Lattner6f286b72005-08-04 01:19:13 +0000142
143 /// getCastedVersionOf - Return the specified value casted to uintptr_t.
144 ///
145 Value *getCastedVersionOf(Value *V);
146private:
Nate Begemanb18121e2004-10-18 21:08:22 +0000147 void runOnLoop(Loop *L);
Chris Lattnereaf24722005-08-04 17:40:30 +0000148 bool AddUsersIfInteresting(Instruction *I, Loop *L,
149 std::set<Instruction*> &Processed);
150 SCEVHandle GetExpressionSCEV(Instruction *E, Loop *L);
151
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000152 void OptimizeIndvars(Loop *L);
Nate Begemane68bcd12005-07-30 00:15:07 +0000153
Chris Lattneredff91a2005-08-10 00:45:21 +0000154 void StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
155 IVUsersOfOneStride &Uses,
Chris Lattner430d0022005-08-03 22:21:05 +0000156 Loop *L, bool isOnlyStride);
Nate Begemanb18121e2004-10-18 21:08:22 +0000157 void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
158 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000159 RegisterOpt<LoopStrengthReduce> X("loop-reduce",
Chris Lattner92233d22005-09-27 21:10:32 +0000160 "Loop Strength Reduction");
Nate Begemanb18121e2004-10-18 21:08:22 +0000161}
162
Jeff Cohena2c59b72005-03-04 04:04:26 +0000163FunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) {
164 return new LoopStrengthReduce(MaxTargetAMSize);
Nate Begemanb18121e2004-10-18 21:08:22 +0000165}
166
Chris Lattner6f286b72005-08-04 01:19:13 +0000167/// getCastedVersionOf - Return the specified value casted to uintptr_t.
168///
169Value *LoopStrengthReduce::getCastedVersionOf(Value *V) {
170 if (V->getType() == UIntPtrTy) return V;
171 if (Constant *CB = dyn_cast<Constant>(V))
172 return ConstantExpr::getCast(CB, UIntPtrTy);
173
174 Value *&New = CastedPointers[V];
175 if (New) return New;
176
177 BasicBlock::iterator InsertPt;
178 if (Argument *Arg = dyn_cast<Argument>(V)) {
179 // Insert into the entry of the function, after any allocas.
180 InsertPt = Arg->getParent()->begin()->begin();
181 while (isa<AllocaInst>(InsertPt)) ++InsertPt;
182 } else {
183 if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
184 InsertPt = II->getNormalDest()->begin();
185 } else {
186 InsertPt = cast<Instruction>(V);
187 ++InsertPt;
188 }
189
190 // Do not insert casts into the middle of PHI node blocks.
191 while (isa<PHINode>(InsertPt)) ++InsertPt;
192 }
Chris Lattneracc42c42005-08-04 19:08:16 +0000193
194 New = new CastInst(V, UIntPtrTy, V->getName(), InsertPt);
195 DeadInsts.insert(cast<Instruction>(New));
196 return New;
Chris Lattner6f286b72005-08-04 01:19:13 +0000197}
198
199
Nate Begemanb18121e2004-10-18 21:08:22 +0000200/// DeleteTriviallyDeadInstructions - If any of the instructions is the
201/// specified set are trivially dead, delete them and see if this makes any of
202/// their operands subsequently dead.
203void LoopStrengthReduce::
204DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
205 while (!Insts.empty()) {
206 Instruction *I = *Insts.begin();
207 Insts.erase(Insts.begin());
208 if (isInstructionTriviallyDead(I)) {
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000209 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
210 if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
211 Insts.insert(U);
Chris Lattner84e9baa2005-08-03 21:36:09 +0000212 SE->deleteInstructionFromRecords(I);
213 I->eraseFromParent();
Nate Begemanb18121e2004-10-18 21:08:22 +0000214 Changed = true;
215 }
216 }
217}
218
Jeff Cohen39751c32005-02-27 19:37:07 +0000219
Chris Lattnereaf24722005-08-04 17:40:30 +0000220/// GetExpressionSCEV - Compute and return the SCEV for the specified
221/// instruction.
222SCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) {
Chris Lattnerc6c4d992005-08-09 23:39:36 +0000223 // Scalar Evolutions doesn't know how to compute SCEV's for GEP instructions.
224 // If this is a GEP that SE doesn't know about, compute it now and insert it.
225 // If this is not a GEP, or if we have already done this computation, just let
226 // SE figure it out.
Chris Lattnereaf24722005-08-04 17:40:30 +0000227 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp);
Chris Lattnerc6c4d992005-08-09 23:39:36 +0000228 if (!GEP || SE->hasSCEV(GEP))
Chris Lattnereaf24722005-08-04 17:40:30 +0000229 return SE->getSCEV(Exp);
230
Nate Begemane68bcd12005-07-30 00:15:07 +0000231 // Analyze all of the subscripts of this getelementptr instruction, looking
232 // for uses that are determined by the trip count of L. First, skip all
233 // operands the are not dependent on the IV.
234
235 // Build up the base expression. Insert an LLVM cast of the pointer to
236 // uintptr_t first.
Chris Lattnereaf24722005-08-04 17:40:30 +0000237 SCEVHandle GEPVal = SCEVUnknown::get(getCastedVersionOf(GEP->getOperand(0)));
Nate Begemane68bcd12005-07-30 00:15:07 +0000238
239 gep_type_iterator GTI = gep_type_begin(GEP);
Chris Lattnereaf24722005-08-04 17:40:30 +0000240
241 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
Nate Begemane68bcd12005-07-30 00:15:07 +0000242 // If this is a use of a recurrence that we can analyze, and it comes before
243 // Op does in the GEP operand list, we will handle this when we process this
244 // operand.
245 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
246 const StructLayout *SL = TD->getStructLayout(STy);
247 unsigned Idx = cast<ConstantUInt>(GEP->getOperand(i))->getValue();
248 uint64_t Offset = SL->MemberOffsets[Idx];
Chris Lattnereaf24722005-08-04 17:40:30 +0000249 GEPVal = SCEVAddExpr::get(GEPVal,
250 SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy));
Nate Begemane68bcd12005-07-30 00:15:07 +0000251 } else {
Chris Lattneracc42c42005-08-04 19:08:16 +0000252 Value *OpVal = getCastedVersionOf(GEP->getOperand(i));
253 SCEVHandle Idx = SE->getSCEV(OpVal);
254
Chris Lattnereaf24722005-08-04 17:40:30 +0000255 uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType());
256 if (TypeSize != 1)
257 Idx = SCEVMulExpr::get(Idx,
258 SCEVConstant::get(ConstantUInt::get(UIntPtrTy,
259 TypeSize)));
260 GEPVal = SCEVAddExpr::get(GEPVal, Idx);
Nate Begemane68bcd12005-07-30 00:15:07 +0000261 }
262 }
263
Chris Lattnerc6c4d992005-08-09 23:39:36 +0000264 SE->setSCEV(GEP, GEPVal);
Chris Lattnereaf24722005-08-04 17:40:30 +0000265 return GEPVal;
Nate Begemane68bcd12005-07-30 00:15:07 +0000266}
267
Chris Lattneracc42c42005-08-04 19:08:16 +0000268/// getSCEVStartAndStride - Compute the start and stride of this expression,
269/// returning false if the expression is not a start/stride pair, or true if it
270/// is. The stride must be a loop invariant expression, but the start may be
271/// a mix of loop invariant and loop variant expressions.
272static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
Chris Lattneredff91a2005-08-10 00:45:21 +0000273 SCEVHandle &Start, SCEVHandle &Stride) {
Chris Lattneracc42c42005-08-04 19:08:16 +0000274 SCEVHandle TheAddRec = Start; // Initialize to zero.
275
276 // If the outer level is an AddExpr, the operands are all start values except
277 // for a nested AddRecExpr.
278 if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) {
279 for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i)
280 if (SCEVAddRecExpr *AddRec =
281 dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) {
282 if (AddRec->getLoop() == L)
283 TheAddRec = SCEVAddExpr::get(AddRec, TheAddRec);
284 else
285 return false; // Nested IV of some sort?
286 } else {
287 Start = SCEVAddExpr::get(Start, AE->getOperand(i));
288 }
289
290 } else if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SH)) {
291 TheAddRec = SH;
292 } else {
293 return false; // not analyzable.
294 }
295
296 SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec);
297 if (!AddRec || AddRec->getLoop() != L) return false;
298
299 // FIXME: Generalize to non-affine IV's.
300 if (!AddRec->isAffine()) return false;
301
302 Start = SCEVAddExpr::get(Start, AddRec->getOperand(0));
303
Chris Lattneracc42c42005-08-04 19:08:16 +0000304 if (!isa<SCEVConstant>(AddRec->getOperand(1)))
Chris Lattneredff91a2005-08-10 00:45:21 +0000305 DEBUG(std::cerr << "[" << L->getHeader()->getName()
306 << "] Variable stride: " << *AddRec << "\n");
Chris Lattneracc42c42005-08-04 19:08:16 +0000307
Chris Lattneredff91a2005-08-10 00:45:21 +0000308 Stride = AddRec->getOperand(1);
309 // Check that all constant strides are the unsigned type, we don't want to
310 // have two IV's one of signed stride 4 and one of unsigned stride 4 to not be
311 // merged.
312 assert((!isa<SCEVConstant>(Stride) || Stride->getType()->isUnsigned()) &&
Chris Lattneracc42c42005-08-04 19:08:16 +0000313 "Constants should be canonicalized to unsigned!");
Chris Lattneredff91a2005-08-10 00:45:21 +0000314
Chris Lattneracc42c42005-08-04 19:08:16 +0000315 return true;
316}
317
Chris Lattnere4ed42a2005-10-03 01:04:44 +0000318/// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression
319/// and now we need to decide whether the user should use the preinc or post-inc
320/// value. If this user should use the post-inc version of the IV, return true.
321///
322/// Choosing wrong here can break dominance properties (if we choose to use the
323/// post-inc value when we cannot) or it can end up adding extra live-ranges to
324/// the loop, resulting in reg-reg copies (if we use the pre-inc value when we
325/// should use the post-inc value).
326static bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV,
Chris Lattnerf07a5872005-10-03 02:50:05 +0000327 Loop *L, DominatorSet *DS, Pass *P) {
Chris Lattnere4ed42a2005-10-03 01:04:44 +0000328 // If the user is in the loop, use the preinc value.
329 if (L->contains(User->getParent())) return false;
330
Chris Lattnerf07a5872005-10-03 02:50:05 +0000331 BasicBlock *LatchBlock = L->getLoopLatch();
332
333 // Ok, the user is outside of the loop. If it is dominated by the latch
334 // block, use the post-inc value.
335 if (DS->dominates(LatchBlock, User->getParent()))
336 return true;
337
338 // There is one case we have to be careful of: PHI nodes. These little guys
339 // can live in blocks that do not dominate the latch block, but (since their
340 // uses occur in the predecessor block, not the block the PHI lives in) should
341 // still use the post-inc value. Check for this case now.
342 PHINode *PN = dyn_cast<PHINode>(User);
343 if (!PN) return false; // not a phi, not dominated by latch block.
344
345 // Look at all of the uses of IV by the PHI node. If any use corresponds to
346 // a block that is not dominated by the latch block, give up and use the
347 // preincremented value.
348 unsigned NumUses = 0;
349 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
350 if (PN->getIncomingValue(i) == IV) {
351 ++NumUses;
352 if (!DS->dominates(LatchBlock, PN->getIncomingBlock(i)))
353 return false;
354 }
355
356 // Okay, all uses of IV by PN are in predecessor blocks that really are
357 // dominated by the latch block. Split the critical edges and use the
358 // post-incremented value.
359 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
360 if (PN->getIncomingValue(i) == IV) {
361 SplitCriticalEdge(PN->getIncomingBlock(i), PN->getParent(), P);
362 if (--NumUses == 0) break;
363 }
364
365 return true;
Chris Lattnere4ed42a2005-10-03 01:04:44 +0000366}
367
368
369
Nate Begemane68bcd12005-07-30 00:15:07 +0000370/// AddUsersIfInteresting - Inspect the specified instruction. If it is a
371/// reducible SCEV, recursively add its users to the IVUsesByStride set and
372/// return true. Otherwise, return false.
Chris Lattnereaf24722005-08-04 17:40:30 +0000373bool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L,
374 std::set<Instruction*> &Processed) {
Chris Lattner5df0e362005-10-21 05:45:41 +0000375 if (!I->getType()->isInteger() && !isa<PointerType>(I->getType()))
376 return false; // Void and FP expressions cannot be reduced.
Chris Lattnereaf24722005-08-04 17:40:30 +0000377 if (!Processed.insert(I).second)
378 return true; // Instruction already handled.
379
Chris Lattneracc42c42005-08-04 19:08:16 +0000380 // Get the symbolic expression for this instruction.
Chris Lattnereaf24722005-08-04 17:40:30 +0000381 SCEVHandle ISE = GetExpressionSCEV(I, L);
Chris Lattneracc42c42005-08-04 19:08:16 +0000382 if (isa<SCEVCouldNotCompute>(ISE)) return false;
Chris Lattnereaf24722005-08-04 17:40:30 +0000383
Chris Lattneracc42c42005-08-04 19:08:16 +0000384 // Get the start and stride for this expression.
385 SCEVHandle Start = SCEVUnknown::getIntegerSCEV(0, ISE->getType());
Chris Lattneredff91a2005-08-10 00:45:21 +0000386 SCEVHandle Stride = Start;
Chris Lattneracc42c42005-08-04 19:08:16 +0000387 if (!getSCEVStartAndStride(ISE, L, Start, Stride))
388 return false; // Non-reducible symbolic expression, bail out.
389
Nate Begemane68bcd12005-07-30 00:15:07 +0000390 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;++UI){
391 Instruction *User = cast<Instruction>(*UI);
392
393 // Do not infinitely recurse on PHI nodes.
Chris Lattnerfd018c82005-09-13 02:09:55 +0000394 if (isa<PHINode>(User) && Processed.count(User))
Nate Begemane68bcd12005-07-30 00:15:07 +0000395 continue;
396
397 // If this is an instruction defined in a nested loop, or outside this loop,
Chris Lattnera0102fb2005-08-04 00:14:11 +0000398 // don't recurse into it.
Chris Lattneracc42c42005-08-04 19:08:16 +0000399 bool AddUserToIVUsers = false;
Chris Lattnera0102fb2005-08-04 00:14:11 +0000400 if (LI->getLoopFor(User->getParent()) != L) {
Chris Lattnerfd018c82005-09-13 02:09:55 +0000401 DEBUG(std::cerr << "FOUND USER in other loop: " << *User
Chris Lattnera0102fb2005-08-04 00:14:11 +0000402 << " OF SCEV: " << *ISE << "\n");
Chris Lattneracc42c42005-08-04 19:08:16 +0000403 AddUserToIVUsers = true;
Chris Lattnereaf24722005-08-04 17:40:30 +0000404 } else if (!AddUsersIfInteresting(User, L, Processed)) {
Chris Lattner65107492005-08-04 00:40:47 +0000405 DEBUG(std::cerr << "FOUND USER: " << *User
406 << " OF SCEV: " << *ISE << "\n");
Chris Lattneracc42c42005-08-04 19:08:16 +0000407 AddUserToIVUsers = true;
408 }
Nate Begemane68bcd12005-07-30 00:15:07 +0000409
Chris Lattneracc42c42005-08-04 19:08:16 +0000410 if (AddUserToIVUsers) {
Chris Lattner4ea0a3e2005-10-09 06:20:55 +0000411 IVUsersOfOneStride &StrideUses = IVUsesByStride[Stride];
412 if (StrideUses.Users.empty()) // First occurance of this stride?
413 StrideOrder.push_back(Stride);
414
Chris Lattner65107492005-08-04 00:40:47 +0000415 // Okay, we found a user that we cannot reduce. Analyze the instruction
Chris Lattnera6764832005-09-12 06:04:47 +0000416 // and decide what to do with it. If we are a use inside of the loop, use
417 // the value before incrementation, otherwise use it after incrementation.
Chris Lattnerf07a5872005-10-03 02:50:05 +0000418 if (IVUseShouldUsePostIncValue(User, I, L, DS, this)) {
Chris Lattnera6764832005-09-12 06:04:47 +0000419 // The value used will be incremented by the stride more than we are
420 // expecting, so subtract this off.
421 SCEVHandle NewStart = SCEV::getMinusSCEV(Start, Stride);
Chris Lattner4ea0a3e2005-10-09 06:20:55 +0000422 StrideUses.addUser(NewStart, User, I);
423 StrideUses.Users.back().isUseOfPostIncrementedValue = true;
Chris Lattnerf07a5872005-10-03 02:50:05 +0000424 DEBUG(std::cerr << " USING POSTINC SCEV, START=" << *NewStart<< "\n");
Chris Lattnere4ed42a2005-10-03 01:04:44 +0000425 } else {
Chris Lattner4ea0a3e2005-10-09 06:20:55 +0000426 StrideUses.addUser(Start, User, I);
Chris Lattnera6764832005-09-12 06:04:47 +0000427 }
Nate Begemane68bcd12005-07-30 00:15:07 +0000428 }
429 }
430 return true;
431}
432
433namespace {
434 /// BasedUser - For a particular base value, keep information about how we've
435 /// partitioned the expression so far.
436 struct BasedUser {
Chris Lattner37c24cc2005-08-08 22:56:21 +0000437 /// Base - The Base value for the PHI node that needs to be inserted for
438 /// this use. As the use is processed, information gets moved from this
439 /// field to the Imm field (below). BasedUser values are sorted by this
440 /// field.
441 SCEVHandle Base;
442
Nate Begemane68bcd12005-07-30 00:15:07 +0000443 /// Inst - The instruction using the induction variable.
444 Instruction *Inst;
445
Chris Lattner430d0022005-08-03 22:21:05 +0000446 /// OperandValToReplace - The operand value of Inst to replace with the
447 /// EmittedBase.
448 Value *OperandValToReplace;
Nate Begemane68bcd12005-07-30 00:15:07 +0000449
450 /// Imm - The immediate value that should be added to the base immediately
451 /// before Inst, because it will be folded into the imm field of the
452 /// instruction.
453 SCEVHandle Imm;
454
455 /// EmittedBase - The actual value* to use for the base value of this
456 /// operation. This is null if we should just use zero so far.
457 Value *EmittedBase;
458
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000459 // isUseOfPostIncrementedValue - True if this should use the
460 // post-incremented version of this IV, not the preincremented version.
461 // This can only be set in special cases, such as the terminating setcc
Chris Lattnera6764832005-09-12 06:04:47 +0000462 // instruction for a loop and uses outside the loop that are dominated by
463 // the loop.
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000464 bool isUseOfPostIncrementedValue;
Chris Lattner37c24cc2005-08-08 22:56:21 +0000465
466 BasedUser(IVStrideUse &IVSU)
467 : Base(IVSU.Offset), Inst(IVSU.User),
468 OperandValToReplace(IVSU.OperandValToReplace),
469 Imm(SCEVUnknown::getIntegerSCEV(0, Base->getType())), EmittedBase(0),
470 isUseOfPostIncrementedValue(IVSU.isUseOfPostIncrementedValue) {}
Nate Begemane68bcd12005-07-30 00:15:07 +0000471
Chris Lattnera6d7c352005-08-04 20:03:32 +0000472 // Once we rewrite the code to insert the new IVs we want, update the
473 // operands of Inst to use the new expression 'NewBase', with 'Imm' added
474 // to it.
Chris Lattnera091ff12005-08-09 00:18:09 +0000475 void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
Chris Lattner8447b492005-08-12 22:22:17 +0000476 SCEVExpander &Rewriter, Loop *L,
477 Pass *P);
Nate Begemane68bcd12005-07-30 00:15:07 +0000478 void dump() const;
479 };
480}
481
482void BasedUser::dump() const {
Chris Lattner37c24cc2005-08-08 22:56:21 +0000483 std::cerr << " Base=" << *Base;
Nate Begemane68bcd12005-07-30 00:15:07 +0000484 std::cerr << " Imm=" << *Imm;
485 if (EmittedBase)
486 std::cerr << " EB=" << *EmittedBase;
487
488 std::cerr << " Inst: " << *Inst;
489}
490
Chris Lattnera6d7c352005-08-04 20:03:32 +0000491// Once we rewrite the code to insert the new IVs we want, update the
492// operands of Inst to use the new expression 'NewBase', with 'Imm' added
493// to it.
Chris Lattnera091ff12005-08-09 00:18:09 +0000494void BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
Chris Lattner4fec86d2005-08-12 22:06:11 +0000495 SCEVExpander &Rewriter,
Chris Lattner8447b492005-08-12 22:22:17 +0000496 Loop *L, Pass *P) {
Chris Lattnera6d7c352005-08-04 20:03:32 +0000497 if (!isa<PHINode>(Inst)) {
Chris Lattnera091ff12005-08-09 00:18:09 +0000498 SCEVHandle NewValSCEV = SCEVAddExpr::get(NewBase, Imm);
Chris Lattnera6d7c352005-08-04 20:03:32 +0000499 Value *NewVal = Rewriter.expandCodeFor(NewValSCEV, Inst,
500 OperandValToReplace->getType());
Chris Lattnera6d7c352005-08-04 20:03:32 +0000501 // Replace the use of the operand Value with the new Phi we just created.
502 Inst->replaceUsesOfWith(OperandValToReplace, NewVal);
503 DEBUG(std::cerr << " CHANGED: IMM =" << *Imm << " Inst = " << *Inst);
504 return;
505 }
506
507 // PHI nodes are more complex. We have to insert one copy of the NewBase+Imm
Chris Lattnerdde7dc52005-08-10 00:35:32 +0000508 // expression into each operand block that uses it. Note that PHI nodes can
509 // have multiple entries for the same predecessor. We use a map to make sure
510 // that a PHI node only has a single Value* for each predecessor (which also
511 // prevents us from inserting duplicate code in some blocks).
512 std::map<BasicBlock*, Value*> InsertedCode;
Chris Lattnera6d7c352005-08-04 20:03:32 +0000513 PHINode *PN = cast<PHINode>(Inst);
514 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
515 if (PN->getIncomingValue(i) == OperandValToReplace) {
Chris Lattner4fec86d2005-08-12 22:06:11 +0000516 // If this is a critical edge, split the edge so that we do not insert the
Chris Lattnerfd018c82005-09-13 02:09:55 +0000517 // code on all predecessor/successor paths. We do this unless this is the
518 // canonical backedge for this loop, as this can make some inserted code
519 // be in an illegal position.
Chris Lattner8fcce172005-10-03 00:31:52 +0000520 BasicBlock *PHIPred = PN->getIncomingBlock(i);
521 if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 &&
522 (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) {
Chris Lattner8fcce172005-10-03 00:31:52 +0000523
Chris Lattner2bf7cb52005-08-17 06:35:16 +0000524 // First step, split the critical edge.
Chris Lattner8fcce172005-10-03 00:31:52 +0000525 SplitCriticalEdge(PHIPred, PN->getParent(), P);
Chris Lattner8447b492005-08-12 22:22:17 +0000526
Chris Lattner2bf7cb52005-08-17 06:35:16 +0000527 // Next step: move the basic block. In particular, if the PHI node
528 // is outside of the loop, and PredTI is in the loop, we want to
529 // move the block to be immediately before the PHI block, not
530 // immediately after PredTI.
Chris Lattner8fcce172005-10-03 00:31:52 +0000531 if (L->contains(PHIPred) && !L->contains(PN->getParent())) {
Chris Lattner2bf7cb52005-08-17 06:35:16 +0000532 BasicBlock *NewBB = PN->getIncomingBlock(i);
533 NewBB->moveBefore(PN->getParent());
Chris Lattner4fec86d2005-08-12 22:06:11 +0000534 }
535 }
Chris Lattnera6d7c352005-08-04 20:03:32 +0000536
Chris Lattnerdde7dc52005-08-10 00:35:32 +0000537 Value *&Code = InsertedCode[PN->getIncomingBlock(i)];
538 if (!Code) {
539 // Insert the code into the end of the predecessor block.
540 BasicBlock::iterator InsertPt =PN->getIncomingBlock(i)->getTerminator();
Chris Lattnera6d7c352005-08-04 20:03:32 +0000541
Chris Lattnerdde7dc52005-08-10 00:35:32 +0000542 SCEVHandle NewValSCEV = SCEVAddExpr::get(NewBase, Imm);
543 Code = Rewriter.expandCodeFor(NewValSCEV, InsertPt,
544 OperandValToReplace->getType());
545 }
Chris Lattnera6d7c352005-08-04 20:03:32 +0000546
547 // Replace the use of the operand Value with the new Phi we just created.
Chris Lattnerdde7dc52005-08-10 00:35:32 +0000548 PN->setIncomingValue(i, Code);
Chris Lattnera6d7c352005-08-04 20:03:32 +0000549 Rewriter.clear();
550 }
551 }
552 DEBUG(std::cerr << " CHANGED: IMM =" << *Imm << " Inst = " << *Inst);
553}
554
555
Nate Begemane68bcd12005-07-30 00:15:07 +0000556/// isTargetConstant - Return true if the following can be referenced by the
557/// immediate field of a target instruction.
558static bool isTargetConstant(const SCEVHandle &V) {
Jeff Cohen546fd592005-07-30 18:33:25 +0000559
Nate Begemane68bcd12005-07-30 00:15:07 +0000560 // FIXME: Look at the target to decide if &GV is a legal constant immediate.
Chris Lattner14203e82005-08-08 06:25:50 +0000561 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
562 // PPC allows a sign-extended 16-bit immediate field.
Chris Lattner07720072005-12-05 18:23:57 +0000563 int64_t V = SC->getValue()->getSExtValue();
564 if (V > -(1 << 16) && V < (1 << 16)-1)
565 return true;
Chris Lattner14203e82005-08-08 06:25:50 +0000566 return false;
567 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000568
Nate Begemane68bcd12005-07-30 00:15:07 +0000569 return false; // ENABLE this for x86
Jeff Cohen546fd592005-07-30 18:33:25 +0000570
Nate Begemane68bcd12005-07-30 00:15:07 +0000571 if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V))
572 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue()))
573 if (CE->getOpcode() == Instruction::Cast)
574 if (isa<GlobalValue>(CE->getOperand(0)))
575 // FIXME: should check to see that the dest is uintptr_t!
576 return true;
577 return false;
578}
579
Chris Lattner37ed8952005-08-08 22:32:34 +0000580/// MoveLoopVariantsToImediateField - Move any subexpressions from Val that are
581/// loop varying to the Imm operand.
582static void MoveLoopVariantsToImediateField(SCEVHandle &Val, SCEVHandle &Imm,
583 Loop *L) {
584 if (Val->isLoopInvariant(L)) return; // Nothing to do.
585
586 if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
587 std::vector<SCEVHandle> NewOps;
588 NewOps.reserve(SAE->getNumOperands());
589
590 for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
591 if (!SAE->getOperand(i)->isLoopInvariant(L)) {
592 // If this is a loop-variant expression, it must stay in the immediate
593 // field of the expression.
594 Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
595 } else {
596 NewOps.push_back(SAE->getOperand(i));
597 }
598
599 if (NewOps.empty())
600 Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
601 else
602 Val = SCEVAddExpr::get(NewOps);
603 } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
604 // Try to pull immediates out of the start value of nested addrec's.
605 SCEVHandle Start = SARE->getStart();
606 MoveLoopVariantsToImediateField(Start, Imm, L);
607
608 std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
609 Ops[0] = Start;
610 Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
611 } else {
612 // Otherwise, all of Val is variant, move the whole thing over.
613 Imm = SCEVAddExpr::get(Imm, Val);
614 Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
615 }
616}
617
618
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000619/// MoveImmediateValues - Look at Val, and pull out any additions of constants
Nate Begemane68bcd12005-07-30 00:15:07 +0000620/// that can fit into the immediate field of instructions in the target.
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000621/// Accumulate these immediate values into the Imm value.
622static void MoveImmediateValues(SCEVHandle &Val, SCEVHandle &Imm,
623 bool isAddress, Loop *L) {
Chris Lattnerfc624702005-08-03 23:44:42 +0000624 if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000625 std::vector<SCEVHandle> NewOps;
626 NewOps.reserve(SAE->getNumOperands());
627
628 for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
Chris Lattneracc42c42005-08-04 19:08:16 +0000629 if (isAddress && isTargetConstant(SAE->getOperand(i))) {
630 Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
631 } else if (!SAE->getOperand(i)->isLoopInvariant(L)) {
632 // If this is a loop-variant expression, it must stay in the immediate
633 // field of the expression.
634 Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000635 } else {
636 NewOps.push_back(SAE->getOperand(i));
Nate Begemane68bcd12005-07-30 00:15:07 +0000637 }
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000638
639 if (NewOps.empty())
640 Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
641 else
642 Val = SCEVAddExpr::get(NewOps);
643 return;
Chris Lattnerfc624702005-08-03 23:44:42 +0000644 } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
645 // Try to pull immediates out of the start value of nested addrec's.
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000646 SCEVHandle Start = SARE->getStart();
647 MoveImmediateValues(Start, Imm, isAddress, L);
648
649 if (Start != SARE->getStart()) {
650 std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
651 Ops[0] = Start;
652 Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
653 }
654 return;
Nate Begemane68bcd12005-07-30 00:15:07 +0000655 }
656
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000657 // Loop-variant expressions must stay in the immediate field of the
658 // expression.
659 if ((isAddress && isTargetConstant(Val)) ||
660 !Val->isLoopInvariant(L)) {
661 Imm = SCEVAddExpr::get(Imm, Val);
662 Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
663 return;
Chris Lattner0f7c0fa2005-08-04 19:26:19 +0000664 }
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000665
666 // Otherwise, no immediates to move.
Nate Begemane68bcd12005-07-30 00:15:07 +0000667}
668
Chris Lattner5949d492005-08-13 07:27:18 +0000669
670/// IncrementAddExprUses - Decompose the specified expression into its added
671/// subexpressions, and increment SubExpressionUseCounts for each of these
672/// decomposed parts.
673static void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs,
674 SCEVHandle Expr) {
675 if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) {
676 for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j)
677 SeparateSubExprs(SubExprs, AE->getOperand(j));
678 } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) {
679 SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Expr->getType());
680 if (SARE->getOperand(0) == Zero) {
681 SubExprs.push_back(Expr);
682 } else {
683 // Compute the addrec with zero as its base.
684 std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
685 Ops[0] = Zero; // Start with zero base.
686 SubExprs.push_back(SCEVAddRecExpr::get(Ops, SARE->getLoop()));
687
688
689 SeparateSubExprs(SubExprs, SARE->getOperand(0));
690 }
691 } else if (!isa<SCEVConstant>(Expr) ||
692 !cast<SCEVConstant>(Expr)->getValue()->isNullValue()) {
693 // Do not add zero.
694 SubExprs.push_back(Expr);
695 }
696}
697
698
Chris Lattnera091ff12005-08-09 00:18:09 +0000699/// RemoveCommonExpressionsFromUseBases - Look through all of the uses in Bases,
700/// removing any common subexpressions from it. Anything truly common is
701/// removed, accumulated, and returned. This looks for things like (a+b+c) and
702/// (a+c+d) -> (a+c). The common expression is *removed* from the Bases.
703static SCEVHandle
704RemoveCommonExpressionsFromUseBases(std::vector<BasedUser> &Uses) {
705 unsigned NumUses = Uses.size();
706
707 // Only one use? Use its base, regardless of what it is!
708 SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Uses[0].Base->getType());
709 SCEVHandle Result = Zero;
710 if (NumUses == 1) {
711 std::swap(Result, Uses[0].Base);
712 return Result;
713 }
714
715 // To find common subexpressions, count how many of Uses use each expression.
716 // If any subexpressions are used Uses.size() times, they are common.
717 std::map<SCEVHandle, unsigned> SubExpressionUseCounts;
718
Chris Lattner192cd182005-10-11 18:41:04 +0000719 // UniqueSubExprs - Keep track of all of the subexpressions we see in the
720 // order we see them.
721 std::vector<SCEVHandle> UniqueSubExprs;
722
Chris Lattner5949d492005-08-13 07:27:18 +0000723 std::vector<SCEVHandle> SubExprs;
724 for (unsigned i = 0; i != NumUses; ++i) {
725 // If the base is zero (which is common), return zero now, there are no
726 // CSEs we can find.
727 if (Uses[i].Base == Zero) return Zero;
728
729 // Split the expression into subexprs.
730 SeparateSubExprs(SubExprs, Uses[i].Base);
731 // Add one to SubExpressionUseCounts for each subexpr present.
732 for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
Chris Lattner192cd182005-10-11 18:41:04 +0000733 if (++SubExpressionUseCounts[SubExprs[j]] == 1)
734 UniqueSubExprs.push_back(SubExprs[j]);
Chris Lattner5949d492005-08-13 07:27:18 +0000735 SubExprs.clear();
736 }
737
Chris Lattner192cd182005-10-11 18:41:04 +0000738 // Now that we know how many times each is used, build Result. Iterate over
739 // UniqueSubexprs so that we have a stable ordering.
740 for (unsigned i = 0, e = UniqueSubExprs.size(); i != e; ++i) {
741 std::map<SCEVHandle, unsigned>::iterator I =
742 SubExpressionUseCounts.find(UniqueSubExprs[i]);
743 assert(I != SubExpressionUseCounts.end() && "Entry not found?");
Chris Lattnera091ff12005-08-09 00:18:09 +0000744 if (I->second == NumUses) { // Found CSE!
745 Result = SCEVAddExpr::get(Result, I->first);
Chris Lattnera091ff12005-08-09 00:18:09 +0000746 } else {
747 // Remove non-cse's from SubExpressionUseCounts.
Chris Lattner192cd182005-10-11 18:41:04 +0000748 SubExpressionUseCounts.erase(I);
Chris Lattnera091ff12005-08-09 00:18:09 +0000749 }
Chris Lattner192cd182005-10-11 18:41:04 +0000750 }
Chris Lattnera091ff12005-08-09 00:18:09 +0000751
752 // If we found no CSE's, return now.
753 if (Result == Zero) return Result;
754
755 // Otherwise, remove all of the CSE's we found from each of the base values.
Chris Lattner5949d492005-08-13 07:27:18 +0000756 for (unsigned i = 0; i != NumUses; ++i) {
757 // Split the expression into subexprs.
758 SeparateSubExprs(SubExprs, Uses[i].Base);
759
760 // Remove any common subexpressions.
761 for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
762 if (SubExpressionUseCounts.count(SubExprs[j])) {
763 SubExprs.erase(SubExprs.begin()+j);
764 --j; --e;
765 }
766
767 // Finally, the non-shared expressions together.
768 if (SubExprs.empty())
Chris Lattnera091ff12005-08-09 00:18:09 +0000769 Uses[i].Base = Zero;
Chris Lattner5949d492005-08-13 07:27:18 +0000770 else
771 Uses[i].Base = SCEVAddExpr::get(SubExprs);
Chris Lattner47d3ec32005-08-13 07:42:01 +0000772 SubExprs.clear();
Chris Lattner5949d492005-08-13 07:27:18 +0000773 }
Chris Lattnera091ff12005-08-09 00:18:09 +0000774
775 return Result;
776}
777
778
Nate Begemane68bcd12005-07-30 00:15:07 +0000779/// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single
780/// stride of IV. All of the users may have different starting values, and this
781/// may not be the only stride (we know it is if isOnlyStride is true).
Chris Lattneredff91a2005-08-10 00:45:21 +0000782void LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
Chris Lattner430d0022005-08-03 22:21:05 +0000783 IVUsersOfOneStride &Uses,
784 Loop *L,
Nate Begemane68bcd12005-07-30 00:15:07 +0000785 bool isOnlyStride) {
786 // Transform our list of users and offsets to a bit more complex table. In
Chris Lattner37c24cc2005-08-08 22:56:21 +0000787 // this new vector, each 'BasedUser' contains 'Base' the base of the
788 // strided accessas well as the old information from Uses. We progressively
789 // move information from the Base field to the Imm field, until we eventually
790 // have the full access expression to rewrite the use.
791 std::vector<BasedUser> UsersToProcess;
Nate Begemane68bcd12005-07-30 00:15:07 +0000792 UsersToProcess.reserve(Uses.Users.size());
Chris Lattner37c24cc2005-08-08 22:56:21 +0000793 for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) {
794 UsersToProcess.push_back(Uses.Users[i]);
795
796 // Move any loop invariant operands from the offset field to the immediate
797 // field of the use, so that we don't try to use something before it is
798 // computed.
799 MoveLoopVariantsToImediateField(UsersToProcess.back().Base,
800 UsersToProcess.back().Imm, L);
801 assert(UsersToProcess.back().Base->isLoopInvariant(L) &&
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000802 "Base value is not loop invariant!");
Nate Begemane68bcd12005-07-30 00:15:07 +0000803 }
Chris Lattner37ed8952005-08-08 22:32:34 +0000804
Chris Lattnera091ff12005-08-09 00:18:09 +0000805 // We now have a whole bunch of uses of like-strided induction variables, but
806 // they might all have different bases. We want to emit one PHI node for this
807 // stride which we fold as many common expressions (between the IVs) into as
808 // possible. Start by identifying the common expressions in the base values
809 // for the strides (e.g. if we have "A+C+B" and "A+B+D" as our bases, find
810 // "A+B"), emit it to the preheader, then remove the expression from the
811 // UsersToProcess base values.
812 SCEVHandle CommonExprs = RemoveCommonExpressionsFromUseBases(UsersToProcess);
813
Chris Lattner37ed8952005-08-08 22:32:34 +0000814 // Next, figure out what we can represent in the immediate fields of
815 // instructions. If we can represent anything there, move it to the imm
Chris Lattnera091ff12005-08-09 00:18:09 +0000816 // fields of the BasedUsers. We do this so that it increases the commonality
817 // of the remaining uses.
Chris Lattner37ed8952005-08-08 22:32:34 +0000818 for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
Chris Lattner5cf983e2005-08-16 00:38:11 +0000819 // If the user is not in the current loop, this means it is using the exit
820 // value of the IV. Do not put anything in the base, make sure it's all in
821 // the immediate field to allow as much factoring as possible.
822 if (!L->contains(UsersToProcess[i].Inst->getParent())) {
Chris Lattnerea7dfd52005-08-17 21:22:41 +0000823 UsersToProcess[i].Imm = SCEVAddExpr::get(UsersToProcess[i].Imm,
824 UsersToProcess[i].Base);
825 UsersToProcess[i].Base =
826 SCEVUnknown::getIntegerSCEV(0, UsersToProcess[i].Base->getType());
Chris Lattner5cf983e2005-08-16 00:38:11 +0000827 } else {
828
829 // Addressing modes can be folded into loads and stores. Be careful that
830 // the store is through the expression, not of the expression though.
831 bool isAddress = isa<LoadInst>(UsersToProcess[i].Inst);
832 if (StoreInst *SI = dyn_cast<StoreInst>(UsersToProcess[i].Inst))
833 if (SI->getOperand(1) == UsersToProcess[i].OperandValToReplace)
834 isAddress = true;
835
836 MoveImmediateValues(UsersToProcess[i].Base, UsersToProcess[i].Imm,
837 isAddress, L);
838 }
Chris Lattner37ed8952005-08-08 22:32:34 +0000839 }
840
Chris Lattnera091ff12005-08-09 00:18:09 +0000841 // Now that we know what we need to do, insert the PHI node itself.
842 //
843 DEBUG(std::cerr << "INSERTING IV of STRIDE " << *Stride << " and BASE "
844 << *CommonExprs << " :\n");
845
846 SCEVExpander Rewriter(*SE, *LI);
847 SCEVExpander PreheaderRewriter(*SE, *LI);
Chris Lattner37ed8952005-08-08 22:32:34 +0000848
Chris Lattnera091ff12005-08-09 00:18:09 +0000849 BasicBlock *Preheader = L->getLoopPreheader();
850 Instruction *PreInsertPt = Preheader->getTerminator();
851 Instruction *PhiInsertBefore = L->getHeader()->begin();
Chris Lattner37ed8952005-08-08 22:32:34 +0000852
Chris Lattner8048b852005-09-12 17:11:27 +0000853 BasicBlock *LatchBlock = L->getLoopLatch();
Chris Lattnerbb78c972005-08-03 23:30:08 +0000854
Chris Lattnera091ff12005-08-09 00:18:09 +0000855 // Create a new Phi for this base, and stick it in the loop header.
856 const Type *ReplacedTy = CommonExprs->getType();
857 PHINode *NewPHI = new PHINode(ReplacedTy, "iv.", PhiInsertBefore);
858 ++NumInserted;
859
Chris Lattneredff91a2005-08-10 00:45:21 +0000860 // Insert the stride into the preheader.
861 Value *StrideV = PreheaderRewriter.expandCodeFor(Stride, PreInsertPt,
862 ReplacedTy);
863 if (!isa<ConstantInt>(StrideV)) ++NumVariable;
864
865
Chris Lattnera091ff12005-08-09 00:18:09 +0000866 // Emit the initial base value into the loop preheader, and add it to the
867 // Phi node.
868 Value *PHIBaseV = PreheaderRewriter.expandCodeFor(CommonExprs, PreInsertPt,
869 ReplacedTy);
870 NewPHI->addIncoming(PHIBaseV, Preheader);
871
872 // Emit the increment of the base value before the terminator of the loop
873 // latch block, and add it to the Phi node.
874 SCEVHandle IncExp = SCEVAddExpr::get(SCEVUnknown::get(NewPHI),
Chris Lattneredff91a2005-08-10 00:45:21 +0000875 SCEVUnknown::get(StrideV));
Chris Lattnera091ff12005-08-09 00:18:09 +0000876
877 Value *IncV = Rewriter.expandCodeFor(IncExp, LatchBlock->getTerminator(),
878 ReplacedTy);
879 IncV->setName(NewPHI->getName()+".inc");
880 NewPHI->addIncoming(IncV, LatchBlock);
881
Chris Lattnerdb23c742005-08-03 22:51:21 +0000882 // Sort by the base value, so that all IVs with identical bases are next to
Chris Lattnera091ff12005-08-09 00:18:09 +0000883 // each other.
Nate Begemane68bcd12005-07-30 00:15:07 +0000884 while (!UsersToProcess.empty()) {
Chris Lattner5c9d63d2005-10-11 18:30:57 +0000885 SCEVHandle Base = UsersToProcess.back().Base;
Chris Lattnerbb78c972005-08-03 23:30:08 +0000886
Chris Lattnera091ff12005-08-09 00:18:09 +0000887 DEBUG(std::cerr << " INSERTING code for BASE = " << *Base << ":\n");
Chris Lattnerbb78c972005-08-03 23:30:08 +0000888
Chris Lattnera091ff12005-08-09 00:18:09 +0000889 // Emit the code for Base into the preheader.
Chris Lattnerc70bbc02005-08-08 05:47:49 +0000890 Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt,
891 ReplacedTy);
Chris Lattnera091ff12005-08-09 00:18:09 +0000892
893 // If BaseV is a constant other than 0, make sure that it gets inserted into
894 // the preheader, instead of being forward substituted into the uses. We do
895 // this by forcing a noop cast to be inserted into the preheader in this
896 // case.
897 if (Constant *C = dyn_cast<Constant>(BaseV))
Chris Lattner530fe6a2005-09-10 01:18:45 +0000898 if (!C->isNullValue() && !isTargetConstant(Base)) {
Chris Lattnera091ff12005-08-09 00:18:09 +0000899 // We want this constant emitted into the preheader!
900 BaseV = new CastInst(BaseV, BaseV->getType(), "preheaderinsert",
901 PreInsertPt);
902 }
903
Nate Begemane68bcd12005-07-30 00:15:07 +0000904 // Emit the code to add the immediate offset to the Phi value, just before
Chris Lattnerdb23c742005-08-03 22:51:21 +0000905 // the instructions that we identified as using this stride and base.
Chris Lattner5c9d63d2005-10-11 18:30:57 +0000906 unsigned ScanPos = 0;
907 do {
908 BasedUser &User = UsersToProcess.back();
Jeff Cohen546fd592005-07-30 18:33:25 +0000909
Chris Lattnera091ff12005-08-09 00:18:09 +0000910 // If this instruction wants to use the post-incremented value, move it
911 // after the post-inc and use its value instead of the PHI.
912 Value *RewriteOp = NewPHI;
913 if (User.isUseOfPostIncrementedValue) {
914 RewriteOp = IncV;
Chris Lattnera6764832005-09-12 06:04:47 +0000915
916 // If this user is in the loop, make sure it is the last thing in the
917 // loop to ensure it is dominated by the increment.
918 if (L->contains(User.Inst->getParent()))
919 User.Inst->moveBefore(LatchBlock->getTerminator());
Chris Lattnera091ff12005-08-09 00:18:09 +0000920 }
921 SCEVHandle RewriteExpr = SCEVUnknown::get(RewriteOp);
922
Chris Lattnerdb23c742005-08-03 22:51:21 +0000923 // Clear the SCEVExpander's expression map so that we are guaranteed
924 // to have the code emitted where we expect it.
925 Rewriter.clear();
Chris Lattnera091ff12005-08-09 00:18:09 +0000926
Chris Lattnera6d7c352005-08-04 20:03:32 +0000927 // Now that we know what we need to do, insert code before User for the
928 // immediate and any loop-variant expressions.
Chris Lattnera091ff12005-08-09 00:18:09 +0000929 if (!isa<ConstantInt>(BaseV) || !cast<ConstantInt>(BaseV)->isNullValue())
930 // Add BaseV to the PHI value if needed.
931 RewriteExpr = SCEVAddExpr::get(RewriteExpr, SCEVUnknown::get(BaseV));
932
Chris Lattner8447b492005-08-12 22:22:17 +0000933 User.RewriteInstructionToUseNewBase(RewriteExpr, Rewriter, L, this);
Jeff Cohen546fd592005-07-30 18:33:25 +0000934
Chris Lattnerdb23c742005-08-03 22:51:21 +0000935 // Mark old value we replaced as possibly dead, so that it is elminated
936 // if we just replaced the last use of that value.
Chris Lattnera6d7c352005-08-04 20:03:32 +0000937 DeadInsts.insert(cast<Instruction>(User.OperandValToReplace));
Nate Begemane68bcd12005-07-30 00:15:07 +0000938
Chris Lattner5c9d63d2005-10-11 18:30:57 +0000939 UsersToProcess.pop_back();
Chris Lattnerdb23c742005-08-03 22:51:21 +0000940 ++NumReduced;
Chris Lattner5c9d63d2005-10-11 18:30:57 +0000941
942 // If there are any more users to process with the same base, move one of
943 // them to the end of the list so that we will process it.
944 if (!UsersToProcess.empty()) {
945 for (unsigned e = UsersToProcess.size(); ScanPos != e; ++ScanPos)
946 if (UsersToProcess[ScanPos].Base == Base) {
947 std::swap(UsersToProcess[ScanPos], UsersToProcess.back());
948 break;
949 }
950 }
951 } while (!UsersToProcess.empty() && UsersToProcess.back().Base == Base);
Nate Begemane68bcd12005-07-30 00:15:07 +0000952 // TODO: Next, find out which base index is the most common, pull it out.
953 }
954
955 // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but
956 // different starting values, into different PHIs.
Nate Begemane68bcd12005-07-30 00:15:07 +0000957}
958
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000959// OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar
960// uses in the loop, look to see if we can eliminate some, in favor of using
961// common indvars for the different uses.
962void LoopStrengthReduce::OptimizeIndvars(Loop *L) {
963 // TODO: implement optzns here.
964
965
966
967
968 // Finally, get the terminating condition for the loop if possible. If we
969 // can, we want to change it to use a post-incremented version of its
970 // induction variable, to allow coallescing the live ranges for the IV into
971 // one register value.
972 PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin());
973 BasicBlock *Preheader = L->getLoopPreheader();
974 BasicBlock *LatchBlock =
975 SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader);
976 BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator());
977 if (!TermBr || TermBr->isUnconditional() ||
978 !isa<SetCondInst>(TermBr->getCondition()))
979 return;
980 SetCondInst *Cond = cast<SetCondInst>(TermBr->getCondition());
981
982 // Search IVUsesByStride to find Cond's IVUse if there is one.
983 IVStrideUse *CondUse = 0;
Chris Lattneredff91a2005-08-10 00:45:21 +0000984 const SCEVHandle *CondStride = 0;
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000985
Chris Lattnerb7a38942005-10-11 18:17:57 +0000986 for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e && !CondUse;
987 ++Stride) {
988 std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
989 IVUsesByStride.find(StrideOrder[Stride]);
990 assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
991
992 for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(),
993 E = SI->second.Users.end(); UI != E; ++UI)
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000994 if (UI->User == Cond) {
995 CondUse = &*UI;
Chris Lattnerb7a38942005-10-11 18:17:57 +0000996 CondStride = &SI->first;
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000997 // NOTE: we could handle setcc instructions with multiple uses here, but
998 // InstCombine does it as well for simple uses, it's not clear that it
999 // occurs enough in real life to handle.
1000 break;
1001 }
Chris Lattnerb7a38942005-10-11 18:17:57 +00001002 }
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001003 if (!CondUse) return; // setcc doesn't use the IV.
1004
1005 // setcc stride is complex, don't mess with users.
Chris Lattneredff91a2005-08-10 00:45:21 +00001006 // FIXME: Evaluate whether this is a good idea or not.
1007 if (!isa<SCEVConstant>(*CondStride)) return;
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001008
1009 // It's possible for the setcc instruction to be anywhere in the loop, and
1010 // possible for it to have multiple users. If it is not immediately before
1011 // the latch block branch, move it.
1012 if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) {
1013 if (Cond->hasOneUse()) { // Condition has a single use, just move it.
1014 Cond->moveBefore(TermBr);
1015 } else {
1016 // Otherwise, clone the terminating condition and insert into the loopend.
1017 Cond = cast<SetCondInst>(Cond->clone());
1018 Cond->setName(L->getHeader()->getName() + ".termcond");
1019 LatchBlock->getInstList().insert(TermBr, Cond);
1020
1021 // Clone the IVUse, as the old use still exists!
Chris Lattneredff91a2005-08-10 00:45:21 +00001022 IVUsesByStride[*CondStride].addUser(CondUse->Offset, Cond,
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001023 CondUse->OperandValToReplace);
Chris Lattneredff91a2005-08-10 00:45:21 +00001024 CondUse = &IVUsesByStride[*CondStride].Users.back();
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001025 }
1026 }
1027
1028 // If we get to here, we know that we can transform the setcc instruction to
1029 // use the post-incremented version of the IV, allowing us to coallesce the
1030 // live ranges for the IV correctly.
Chris Lattneredff91a2005-08-10 00:45:21 +00001031 CondUse->Offset = SCEV::getMinusSCEV(CondUse->Offset, *CondStride);
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001032 CondUse->isUseOfPostIncrementedValue = true;
1033}
Nate Begemane68bcd12005-07-30 00:15:07 +00001034
Nate Begemanb18121e2004-10-18 21:08:22 +00001035void LoopStrengthReduce::runOnLoop(Loop *L) {
1036 // First step, transform all loops nesting inside of this loop.
1037 for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I)
1038 runOnLoop(*I);
1039
Nate Begemane68bcd12005-07-30 00:15:07 +00001040 // Next, find all uses of induction variables in this loop, and catagorize
1041 // them by stride. Start by finding all of the PHI nodes in the header for
1042 // this loop. If they are induction variables, inspect their uses.
Chris Lattnereaf24722005-08-04 17:40:30 +00001043 std::set<Instruction*> Processed; // Don't reprocess instructions.
Nate Begemane68bcd12005-07-30 00:15:07 +00001044 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
Chris Lattnereaf24722005-08-04 17:40:30 +00001045 AddUsersIfInteresting(I, L, Processed);
Nate Begemanb18121e2004-10-18 21:08:22 +00001046
Nate Begemane68bcd12005-07-30 00:15:07 +00001047 // If we have nothing to do, return.
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001048 if (IVUsesByStride.empty()) return;
1049
1050 // Optimize induction variables. Some indvar uses can be transformed to use
1051 // strides that will be needed for other purposes. A common example of this
1052 // is the exit test for the loop, which can often be rewritten to use the
1053 // computation of some other indvar to decide when to terminate the loop.
1054 OptimizeIndvars(L);
1055
Misha Brukmanb1c93172005-04-21 23:48:37 +00001056
Nate Begemane68bcd12005-07-30 00:15:07 +00001057 // FIXME: We can widen subreg IV's here for RISC targets. e.g. instead of
1058 // doing computation in byte values, promote to 32-bit values if safe.
1059
1060 // FIXME: Attempt to reuse values across multiple IV's. In particular, we
1061 // could have something like "for(i) { foo(i*8); bar(i*16) }", which should be
1062 // codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC. Need
1063 // to be careful that IV's are all the same type. Only works for intptr_t
1064 // indvars.
1065
1066 // If we only have one stride, we can more aggressively eliminate some things.
1067 bool HasOneStride = IVUsesByStride.size() == 1;
Chris Lattner4ea0a3e2005-10-09 06:20:55 +00001068
Chris Lattnera091ff12005-08-09 00:18:09 +00001069 // Note: this processes each stride/type pair individually. All users passed
Chris Lattner4ea0a3e2005-10-09 06:20:55 +00001070 // into StrengthReduceStridedIVUsers have the same type AND stride. Also,
1071 // node that we iterate over IVUsesByStride indirectly by using StrideOrder.
1072 // This extra layer of indirection makes the ordering of strides deterministic
1073 // - not dependent on map order.
1074 for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) {
1075 std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
1076 IVUsesByStride.find(StrideOrder[Stride]);
1077 assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
Nate Begemane68bcd12005-07-30 00:15:07 +00001078 StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride);
Chris Lattner4ea0a3e2005-10-09 06:20:55 +00001079 }
Nate Begemanb18121e2004-10-18 21:08:22 +00001080
1081 // Clean up after ourselves
1082 if (!DeadInsts.empty()) {
1083 DeleteTriviallyDeadInstructions(DeadInsts);
1084
Nate Begemane68bcd12005-07-30 00:15:07 +00001085 BasicBlock::iterator I = L->getHeader()->begin();
1086 PHINode *PN;
Chris Lattnerdcce49e2005-08-02 02:44:31 +00001087 while ((PN = dyn_cast<PHINode>(I))) {
Chris Lattner564900e2005-08-02 00:41:11 +00001088 ++I; // Preincrement iterator to avoid invalidating it when deleting PN.
1089
Chris Lattnerc6c4d992005-08-09 23:39:36 +00001090 // At this point, we know that we have killed one or more GEP
1091 // instructions. It is worth checking to see if the cann indvar is also
1092 // dead, so that we can remove it as well. The requirements for the cann
1093 // indvar to be considered dead are:
Nate Begemane68bcd12005-07-30 00:15:07 +00001094 // 1. the cann indvar has one use
1095 // 2. the use is an add instruction
1096 // 3. the add has one use
1097 // 4. the add is used by the cann indvar
1098 // If all four cases above are true, then we can remove both the add and
1099 // the cann indvar.
1100 // FIXME: this needs to eliminate an induction variable even if it's being
1101 // compared against some value to decide loop termination.
1102 if (PN->hasOneUse()) {
1103 BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin()));
Chris Lattner75a44e12005-08-02 02:52:02 +00001104 if (BO && BO->hasOneUse()) {
1105 if (PN == *(BO->use_begin())) {
1106 DeadInsts.insert(BO);
1107 // Break the cycle, then delete the PHI.
1108 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
Chris Lattner84e9baa2005-08-03 21:36:09 +00001109 SE->deleteInstructionFromRecords(PN);
Chris Lattner75a44e12005-08-02 02:52:02 +00001110 PN->eraseFromParent();
Nate Begemanb18121e2004-10-18 21:08:22 +00001111 }
Chris Lattner75a44e12005-08-02 02:52:02 +00001112 }
Nate Begemane68bcd12005-07-30 00:15:07 +00001113 }
Nate Begemanb18121e2004-10-18 21:08:22 +00001114 }
Nate Begemane68bcd12005-07-30 00:15:07 +00001115 DeleteTriviallyDeadInstructions(DeadInsts);
Nate Begemanb18121e2004-10-18 21:08:22 +00001116 }
Nate Begemane68bcd12005-07-30 00:15:07 +00001117
Chris Lattner11e7a5e2005-08-05 01:30:11 +00001118 CastedPointers.clear();
Nate Begemane68bcd12005-07-30 00:15:07 +00001119 IVUsesByStride.clear();
Chris Lattner4ea0a3e2005-10-09 06:20:55 +00001120 StrideOrder.clear();
Nate Begemane68bcd12005-07-30 00:15:07 +00001121 return;
Nate Begemanb18121e2004-10-18 21:08:22 +00001122}