blob: a77bcb95864ca1b9331b2d061dbabd83094c77cf [file] [log] [blame]
Chris Lattnerca081252001-12-14 16:52:21 +00001//===- InstructionCombining.cpp - Combine multiple instructions -------------=//
2//
3// InstructionCombining - Combine instructions to form fewer, simple
4// instructions. This pass does not modify the CFG, and has a tendancy to
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00005// make instructions dead, so a subsequent DIE pass is useful. This pass is
6// where algebraic simplification happens.
Chris Lattnerca081252001-12-14 16:52:21 +00007//
8// This pass combines things like:
9// %Y = add int 1, %X
10// %Z = add int 1, %Y
11// into:
12// %Z = add int 2, %X
13//
14// This is a simple worklist driven algorithm.
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000019#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattner65b529f2002-04-08 20:18:09 +000020#include "llvm/ConstantHandling.h"
Chris Lattnerca081252001-12-14 16:52:21 +000021#include "llvm/iMemory.h"
Chris Lattner3a60d042002-04-15 19:45:29 +000022#include "llvm/iOther.h"
Chris Lattnerbbbdd852002-05-06 18:06:38 +000023#include "llvm/iPHINode.h"
Chris Lattner260ab202002-04-18 17:39:14 +000024#include "llvm/iOperators.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000025#include "llvm/Pass.h"
Chris Lattner60a65912002-02-12 21:07:25 +000026#include "llvm/Support/InstIterator.h"
Chris Lattner260ab202002-04-18 17:39:14 +000027#include "llvm/Support/InstVisitor.h"
Chris Lattnerca081252001-12-14 16:52:21 +000028
Chris Lattnerca081252001-12-14 16:52:21 +000029
Chris Lattner260ab202002-04-18 17:39:14 +000030namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +000031 class InstCombiner : public FunctionPass,
Chris Lattner260ab202002-04-18 17:39:14 +000032 public InstVisitor<InstCombiner, Instruction*> {
33 // Worklist of all of the instructions that need to be simplified.
34 std::vector<Instruction*> WorkList;
35
36 void AddUsesToWorkList(Instruction *I) {
37 // The instruction was simplified, add all users of the instruction to
38 // the work lists because they might get more simplified now...
39 //
40 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
41 UI != UE; ++UI)
42 WorkList.push_back(cast<Instruction>(*UI));
43 }
44
45 public:
Chris Lattner37104aa2002-04-29 14:57:45 +000046 const char *getPassName() const { return "Instruction Combining"; }
47
Chris Lattnerc8e66542002-04-27 06:56:12 +000048 virtual bool runOnFunction(Function *F);
Chris Lattner260ab202002-04-18 17:39:14 +000049
Chris Lattnerf12cc842002-04-28 21:27:06 +000050 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 AU.preservesCFG();
52 }
53
Chris Lattner260ab202002-04-18 17:39:14 +000054 // Visitation implementation - Implement instruction combining for different
55 // instruction types. The semantics are as follows:
56 // Return Value:
57 // null - No change was made
58 // I - Change was made, I is still valid
59 // otherwise - Change was made, replace I with returned instruction
60 //
Chris Lattner5d6bec52002-05-06 17:03:21 +000061 Instruction *visitNot(UnaryOperator *I);
Chris Lattner260ab202002-04-18 17:39:14 +000062 Instruction *visitAdd(BinaryOperator *I);
63 Instruction *visitSub(BinaryOperator *I);
64 Instruction *visitMul(BinaryOperator *I);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +000065 Instruction *visitDiv(BinaryOperator *I);
66 Instruction *visitRem(BinaryOperator *I);
67 Instruction *visitAnd(BinaryOperator *I);
68 Instruction *visitOr (BinaryOperator *I);
69 Instruction *visitXor(BinaryOperator *I);
70 Instruction *visitSetCondInst(BinaryOperator *I);
71 Instruction *visitShiftInst(Instruction *I);
Chris Lattner260ab202002-04-18 17:39:14 +000072 Instruction *visitCastInst(CastInst *CI);
Chris Lattnerbbbdd852002-05-06 18:06:38 +000073 Instruction *visitPHINode(PHINode *PN);
Chris Lattner48a44f72002-05-02 17:06:02 +000074 Instruction *visitGetElementPtrInst(GetElementPtrInst *GEP);
Chris Lattner260ab202002-04-18 17:39:14 +000075 Instruction *visitMemAccessInst(MemAccessInst *MAI);
76
77 // visitInstruction - Specify what to return for unhandled instructions...
78 Instruction *visitInstruction(Instruction *I) { return 0; }
79 };
80}
81
82
Chris Lattner5d6bec52002-05-06 17:03:21 +000083Instruction *InstCombiner::visitNot(UnaryOperator *I) {
84 if (I->use_empty()) return 0; // Don't fix dead instructions...
85
86 // not (not X) = X
87 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(0)))
88 if (Op->getOpcode() == Instruction::Not) {
89 AddUsesToWorkList(I); // Add all modified instrs to worklist
90 I->replaceAllUsesWith(Op->getOperand(0));
91 return I;
92 }
93 return 0;
94}
95
Chris Lattner260ab202002-04-18 17:39:14 +000096
97// Make sure that this instruction has a constant on the right hand side if it
98// has any constant arguments. If not, fix it an return true.
99//
100static bool SimplifyBinOp(BinaryOperator *I) {
Chris Lattnerca081252001-12-14 16:52:21 +0000101 if (isa<Constant>(I->getOperand(0)) && !isa<Constant>(I->getOperand(1)))
Chris Lattner31ba1292002-04-29 22:24:47 +0000102 return !I->swapOperands();
Chris Lattner260ab202002-04-18 17:39:14 +0000103 return false;
104}
Chris Lattnerca081252001-12-14 16:52:21 +0000105
Chris Lattner9fa53de2002-05-06 16:49:18 +0000106// dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
107// instruction if the LHS is a constant zero (which is the 'negate' form).
108//
109static inline Value *dyn_castNegInst(Value *V) {
110 Instruction *I = dyn_cast<Instruction>(V);
111 if (!I || I->getOpcode() != Instruction::Sub) return 0;
112
113 if (I->getOperand(0) == Constant::getNullValue(I->getType()))
114 return I->getOperand(1);
115 return 0;
116}
117
Chris Lattner260ab202002-04-18 17:39:14 +0000118Instruction *InstCombiner::visitAdd(BinaryOperator *I) {
119 if (I->use_empty()) return 0; // Don't fix dead add instructions...
120 bool Changed = SimplifyBinOp(I);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000121 Value *LHS = I->getOperand(0), *RHS = I->getOperand(1);
122
123 // Eliminate 'add int %X, 0'
124 if (I->getType()->isIntegral() &&
125 RHS == Constant::getNullValue(I->getType())) {
126 AddUsesToWorkList(I); // Add all modified instrs to worklist
127 I->replaceAllUsesWith(LHS);
128 return I;
129 }
130
Chris Lattner147e9752002-05-08 22:46:53 +0000131 // -A + B --> B - A
Chris Lattner9fa53de2002-05-06 16:49:18 +0000132 if (Value *V = dyn_castNegInst(LHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000133 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000134
135 // A + -B --> A - B
136 if (Value *V = dyn_castNegInst(RHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000137 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattner260ab202002-04-18 17:39:14 +0000138
139 // Simplify add instructions with a constant RHS...
Chris Lattner9fa53de2002-05-06 16:49:18 +0000140 if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
141 if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
142 if (ILHS->getOpcode() == Instruction::Add &&
143 isa<Constant>(ILHS->getOperand(1))) {
Chris Lattner260ab202002-04-18 17:39:14 +0000144 // Fold:
145 // %Y = add int %X, 1
146 // %Z = add int %Y, 1
147 // into:
148 // %Z = add int %X, 2
149 //
Chris Lattner9fa53de2002-05-06 16:49:18 +0000150 if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
151 I->setOperand(0, ILHS->getOperand(0));
Chris Lattner260ab202002-04-18 17:39:14 +0000152 I->setOperand(1, Val);
Chris Lattnerbee86622002-03-11 23:28:45 +0000153 return I;
Chris Lattner04805fa2002-02-26 21:46:54 +0000154 }
Chris Lattnerca081252001-12-14 16:52:21 +0000155 }
156 }
Chris Lattnerca081252001-12-14 16:52:21 +0000157 }
158
Chris Lattner260ab202002-04-18 17:39:14 +0000159 return Changed ? I : 0;
160}
161
162Instruction *InstCombiner::visitSub(BinaryOperator *I) {
163 if (I->use_empty()) return 0; // Don't fix dead add instructions...
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000164 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
165
166 if (Op0 == Op1) { // sub X, X -> 0
167 AddUsesToWorkList(I); // Add all modified instrs to worklist
168 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
169 return I;
170 }
Chris Lattner260ab202002-04-18 17:39:14 +0000171
172 // If this is a subtract instruction with a constant RHS, convert it to an add
173 // instruction of a negative constant
174 //
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000175 if (Constant *Op2 = dyn_cast<Constant>(Op1))
176 if (Constant *RHS = *Constant::getNullValue(I->getType()) - *Op2) // 0 - RHS
177 return BinaryOperator::create(Instruction::Add, Op0, RHS, I->getName());
Chris Lattner260ab202002-04-18 17:39:14 +0000178
Chris Lattner147e9752002-05-08 22:46:53 +0000179 // If this is a 'C = x-B', check to see if 'B = -A', so that C = x+A...
180 if (Value *V = dyn_castNegInst(Op1))
181 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000182
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000183 return 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000184}
185
186Instruction *InstCombiner::visitMul(BinaryOperator *I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000187 if (I->use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner260ab202002-04-18 17:39:14 +0000188 bool Changed = SimplifyBinOp(I);
189 Value *Op1 = I->getOperand(0);
190
191 // Simplify add instructions with a constant RHS...
192 if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(1))) {
193 if (I->getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){
194 // Eliminate 'mul int %X, 1'
195 AddUsesToWorkList(I); // Add all modified instrs to worklist
196 I->replaceAllUsesWith(Op1);
197 return I;
Chris Lattner31ba1292002-04-29 22:24:47 +0000198
199 } else if (I->getType()->isIntegral() &&
200 cast<ConstantInt>(Op2)->equalsInt(2)) {
201 // Convert 'mul int %X, 2' to 'add int %X, %X'
202 return BinaryOperator::create(Instruction::Add, Op1, Op1, I->getName());
203
204 } else if (Op2->isNullValue()) {
205 // Eliminate 'mul int %X, 0'
206 AddUsesToWorkList(I); // Add all modified instrs to worklist
207 I->replaceAllUsesWith(Op2); // Set this value to zero directly
208 return I;
Chris Lattner260ab202002-04-18 17:39:14 +0000209 }
210 }
211
212 return Changed ? I : 0;
213}
214
215
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000216Instruction *InstCombiner::visitDiv(BinaryOperator *I) {
217 if (I->use_empty()) return 0; // Don't fix dead instructions...
218
219 // div X, 1 == X
220 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1)))
221 if (RHS->equalsInt(1)) {
222 AddUsesToWorkList(I); // Add all modified instrs to worklist
223 I->replaceAllUsesWith(I->getOperand(0));
224 return I;
225 }
226 return 0;
227}
228
229
230Instruction *InstCombiner::visitRem(BinaryOperator *I) {
231 if (I->use_empty()) return 0; // Don't fix dead instructions...
232
233 // rem X, 1 == 0
234 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1)))
235 if (RHS->equalsInt(1)) {
236 AddUsesToWorkList(I); // Add all modified instrs to worklist
237 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
238 return I;
239 }
240 return 0;
241}
242
243static Constant *getMaxValue(const Type *Ty) {
244 assert(Ty == Type::BoolTy || Ty->isIntegral());
245 if (Ty == Type::BoolTy)
246 return ConstantBool::True;
247
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000248 if (Ty->isSigned())
Chris Lattnera6e047a2002-05-06 18:54:59 +0000249 return ConstantSInt::get(Ty, -1);
250 else if (Ty->isUnsigned()) {
251 // Calculate -1 casted to the right type...
252 unsigned TypeBits = Ty->getPrimitiveSize()*8;
253 uint64_t Val = (uint64_t)-1LL; // All ones
254 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000255 return ConstantUInt::get(Ty, Val);
Chris Lattnera6e047a2002-05-06 18:54:59 +0000256 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000257 return 0;
258}
259
260
261Instruction *InstCombiner::visitAnd(BinaryOperator *I) {
262 if (I->use_empty()) return 0; // Don't fix dead instructions...
263 bool Changed = SimplifyBinOp(I);
264 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
265
266 // and X, X = X and X, 0 == 0
267 if (Op0 == Op1 || Op1 == Constant::getNullValue(I->getType())) {
268 AddUsesToWorkList(I); // Add all modified instrs to worklist
269 I->replaceAllUsesWith(Op1);
270 return I;
271 }
272
273 // and X, -1 == X
274 if (Constant *RHS = dyn_cast<Constant>(Op1))
275 if (RHS == getMaxValue(I->getType())) {
276 AddUsesToWorkList(I); // Add all modified instrs to worklist
277 I->replaceAllUsesWith(Op0);
278 return I;
279 }
280
281 return Changed ? I : 0;
282}
283
284
285
286Instruction *InstCombiner::visitOr(BinaryOperator *I) {
287 if (I->use_empty()) return 0; // Don't fix dead instructions...
288 bool Changed = SimplifyBinOp(I);
289 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
290
291 // or X, X = X or X, 0 == X
292 if (Op0 == Op1 || Op1 == Constant::getNullValue(I->getType())) {
293 AddUsesToWorkList(I); // Add all modified instrs to worklist
294 I->replaceAllUsesWith(Op0);
295 return I;
296 }
297
298 // or X, -1 == -1
299 if (Constant *RHS = dyn_cast<Constant>(Op1))
300 if (RHS == getMaxValue(I->getType())) {
301 AddUsesToWorkList(I); // Add all modified instrs to worklist
302 I->replaceAllUsesWith(Op1);
303 return I;
304 }
305
306 return Changed ? I : 0;
307}
308
309
310
311Instruction *InstCombiner::visitXor(BinaryOperator *I) {
312 if (I->use_empty()) return 0; // Don't fix dead instructions...
313 bool Changed = SimplifyBinOp(I);
314 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
315
316 // xor X, X = 0
317 if (Op0 == Op1) {
318 AddUsesToWorkList(I); // Add all modified instrs to worklist
319 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
320 return I;
321 }
322
323 // xor X, 0 == X
324 if (Op1 == Constant::getNullValue(I->getType())) {
325 AddUsesToWorkList(I); // Add all modified instrs to worklist
326 I->replaceAllUsesWith(Op0);
327 return I;
328 }
329
330 return Changed ? I : 0;
331}
332
333Instruction *InstCombiner::visitSetCondInst(BinaryOperator *I) {
334 if (I->use_empty()) return 0; // Don't fix dead instructions...
335 bool Changed = SimplifyBinOp(I);
336
337 // setcc X, X
338 if (I->getOperand(0) == I->getOperand(1)) {
339 bool NewVal = I->getOpcode() == Instruction::SetEQ ||
340 I->getOpcode() == Instruction::SetGE ||
341 I->getOpcode() == Instruction::SetLE;
342 AddUsesToWorkList(I); // Add all modified instrs to worklist
343 I->replaceAllUsesWith(ConstantBool::get(NewVal));
344 return I;
345 }
346
347 return Changed ? I : 0;
348}
349
350
351
352Instruction *InstCombiner::visitShiftInst(Instruction *I) {
353 if (I->use_empty()) return 0; // Don't fix dead instructions...
354 assert(I->getOperand(1)->getType() == Type::UByteTy);
355 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
356
357 // shl X, 0 == X and shr X, 0 == X
358 // shl 0, X == 0 and shr 0, X == 0
359 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
360 Op0 == Constant::getNullValue(Op0->getType())) {
361 AddUsesToWorkList(I); // Add all modified instrs to worklist
362 I->replaceAllUsesWith(Op0);
363 return I;
364 }
365
366 // shl int X, 32 = 0 and shr sbyte Y, 9 = 0, ... just don't eliminate shr of
367 // a signed value.
368 //
369 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
370 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
371 if (CUI->getValue() >= TypeBits &&
372 !(Op0->getType()->isSigned() && I->getOpcode() == Instruction::Shr)) {
373 AddUsesToWorkList(I); // Add all modified instrs to worklist
374 I->replaceAllUsesWith(Constant::getNullValue(Op0->getType()));
375 return I;
376 }
377 }
378 return 0;
379}
380
381
Chris Lattner48a44f72002-05-02 17:06:02 +0000382// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
383// instruction.
384//
385static inline bool isEliminableCastOfCast(const CastInst *CI,
386 const CastInst *CSrc) {
387 assert(CI->getOperand(0) == CSrc);
388 const Type *SrcTy = CSrc->getOperand(0)->getType();
389 const Type *MidTy = CSrc->getType();
390 const Type *DstTy = CI->getType();
391
392 // It is legal to eliminate the instruction if casting A->B->A
393 if (SrcTy == DstTy) return true;
394
395 // Allow free casting and conversion of sizes as long as the sign doesn't
396 // change...
397 if (SrcTy->isSigned() == MidTy->isSigned() &&
398 MidTy->isSigned() == DstTy->isSigned())
399 return true;
400
401 // Otherwise, we cannot succeed. Specifically we do not want to allow things
402 // like: short -> ushort -> uint, because this can create wrong results if
403 // the input short is negative!
404 //
405 return false;
406}
407
408
409// CastInst simplification
Chris Lattner260ab202002-04-18 17:39:14 +0000410//
411Instruction *InstCombiner::visitCastInst(CastInst *CI) {
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000412 if (CI->use_empty()) return 0; // Don't fix dead instructions...
413
Chris Lattner48a44f72002-05-02 17:06:02 +0000414 // If the user is casting a value to the same type, eliminate this cast
415 // instruction...
Chris Lattner260ab202002-04-18 17:39:14 +0000416 if (CI->getType() == CI->getOperand(0)->getType() && !CI->use_empty()) {
417 AddUsesToWorkList(CI); // Add all modified instrs to worklist
418 CI->replaceAllUsesWith(CI->getOperand(0));
419 return CI;
420 }
Chris Lattner48a44f72002-05-02 17:06:02 +0000421
422
423 // If casting the result of another cast instruction, try to eliminate this
424 // one!
425 //
426 if (CastInst *CSrc = dyn_cast<CastInst>(CI->getOperand(0)))
427 if (isEliminableCastOfCast(CI, CSrc)) {
428 // This instruction now refers directly to the cast's src operand. This
429 // has a good chance of making CSrc dead.
430 CI->setOperand(0, CSrc->getOperand(0));
431 return CI;
432 }
433
Chris Lattner260ab202002-04-18 17:39:14 +0000434 return 0;
Chris Lattnerca081252001-12-14 16:52:21 +0000435}
436
Chris Lattner48a44f72002-05-02 17:06:02 +0000437
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000438// PHINode simplification
439//
440Instruction *InstCombiner::visitPHINode(PHINode *PN) {
441 if (PN->use_empty()) return 0; // Don't fix dead instructions...
442
443 // If the PHI node only has one incoming value, eliminate the PHI node...
444 if (PN->getNumIncomingValues() == 1) {
445 AddUsesToWorkList(PN); // Add all modified instrs to worklist
446 PN->replaceAllUsesWith(PN->getIncomingValue(0));
447 return PN;
448 }
449
450 return 0;
451}
452
Chris Lattner48a44f72002-05-02 17:06:02 +0000453
454Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst *GEP) {
455 // Is it getelementptr %P, uint 0
456 // If so, elminate the noop.
457 if (GEP->getNumOperands() == 2 && !GEP->use_empty() &&
458 GEP->getOperand(1) == Constant::getNullValue(Type::UIntTy)) {
459 AddUsesToWorkList(GEP); // Add all modified instrs to worklist
460 GEP->replaceAllUsesWith(GEP->getOperand(0));
461 return GEP;
462 }
463
464 return visitMemAccessInst(GEP);
465}
466
467
Chris Lattnerca081252001-12-14 16:52:21 +0000468// Combine Indices - If the source pointer to this mem access instruction is a
469// getelementptr instruction, combine the indices of the GEP into this
470// instruction
471//
Chris Lattner260ab202002-04-18 17:39:14 +0000472Instruction *InstCombiner::visitMemAccessInst(MemAccessInst *MAI) {
Chris Lattnerca081252001-12-14 16:52:21 +0000473 GetElementPtrInst *Src =
474 dyn_cast<GetElementPtrInst>(MAI->getPointerOperand());
475 if (!Src) return 0;
476
Chris Lattner7f74a562002-01-20 22:54:45 +0000477 std::vector<Value *> Indices;
Chris Lattnerca081252001-12-14 16:52:21 +0000478
479 // Only special case we have to watch out for is pointer arithmetic on the
480 // 0th index of MAI.
481 unsigned FirstIdx = MAI->getFirstIndexOperandNumber();
482 if (FirstIdx == MAI->getNumOperands() ||
483 (FirstIdx == MAI->getNumOperands()-1 &&
484 MAI->getOperand(FirstIdx) == ConstantUInt::get(Type::UIntTy, 0))) {
485 // Replace the index list on this MAI with the index on the getelementptr
486 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
487 } else if (*MAI->idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) {
488 // Otherwise we can do the fold if the first index of the GEP is a zero
489 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
490 Indices.insert(Indices.end(), MAI->idx_begin()+1, MAI->idx_end());
491 }
492
493 if (Indices.empty()) return 0; // Can't do the fold?
494
495 switch (MAI->getOpcode()) {
496 case Instruction::GetElementPtr:
497 return new GetElementPtrInst(Src->getOperand(0), Indices, MAI->getName());
498 case Instruction::Load:
499 return new LoadInst(Src->getOperand(0), Indices, MAI->getName());
500 case Instruction::Store:
Chris Lattnerf40379e2002-04-18 14:43:54 +0000501 return new StoreInst(MAI->getOperand(0), Src->getOperand(0), Indices);
Chris Lattnerca081252001-12-14 16:52:21 +0000502 default:
503 assert(0 && "Unknown memaccessinst!");
504 break;
505 }
506 abort();
507 return 0;
508}
509
Chris Lattnerca081252001-12-14 16:52:21 +0000510
Chris Lattnerc8e66542002-04-27 06:56:12 +0000511bool InstCombiner::runOnFunction(Function *F) {
Chris Lattner260ab202002-04-18 17:39:14 +0000512 bool Changed = false;
Chris Lattnerca081252001-12-14 16:52:21 +0000513
Chris Lattner260ab202002-04-18 17:39:14 +0000514 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattnerca081252001-12-14 16:52:21 +0000515
516 while (!WorkList.empty()) {
517 Instruction *I = WorkList.back(); // Get an instruction from the worklist
518 WorkList.pop_back();
519
520 // Now that we have an instruction, try combining it to simplify it...
Chris Lattner260ab202002-04-18 17:39:14 +0000521 Instruction *Result = visit(I);
522 if (Result) {
523 // Should we replace the old instruction with a new one?
524 if (Result != I)
525 ReplaceInstWithInst(I, Result);
526
527 WorkList.push_back(Result);
528 AddUsesToWorkList(Result);
529 Changed = true;
Chris Lattnerca081252001-12-14 16:52:21 +0000530 }
531 }
532
Chris Lattner260ab202002-04-18 17:39:14 +0000533 return Changed;
Chris Lattner04805fa2002-02-26 21:46:54 +0000534}
535
536Pass *createInstructionCombiningPass() {
Chris Lattner260ab202002-04-18 17:39:14 +0000537 return new InstCombiner();
Chris Lattner04805fa2002-02-26 21:46:54 +0000538}