blob: 0812fca6e97994ba07374b10fb1f70c2a55004f1 [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 Lattner0b18c1d2002-05-10 15:38:35 +000028#include "Support/StatisticReporter.h"
Chris Lattner053c0932002-05-14 15:24:07 +000029#include <algorithm>
Chris Lattnerca081252001-12-14 16:52:21 +000030
Chris Lattner0b18c1d2002-05-10 15:38:35 +000031static Statistic<> NumCombined("instcombine\t- Number of insts combined");
Chris Lattnerca081252001-12-14 16:52:21 +000032
Chris Lattner260ab202002-04-18 17:39:14 +000033namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +000034 class InstCombiner : public FunctionPass,
Chris Lattner260ab202002-04-18 17:39:14 +000035 public InstVisitor<InstCombiner, Instruction*> {
36 // Worklist of all of the instructions that need to be simplified.
37 std::vector<Instruction*> WorkList;
38
39 void AddUsesToWorkList(Instruction *I) {
40 // The instruction was simplified, add all users of the instruction to
41 // the work lists because they might get more simplified now...
42 //
43 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
44 UI != UE; ++UI)
45 WorkList.push_back(cast<Instruction>(*UI));
46 }
47
48 public:
Chris Lattner37104aa2002-04-29 14:57:45 +000049 const char *getPassName() const { return "Instruction Combining"; }
50
Chris Lattnerc8e66542002-04-27 06:56:12 +000051 virtual bool runOnFunction(Function *F);
Chris Lattner260ab202002-04-18 17:39:14 +000052
Chris Lattnerf12cc842002-04-28 21:27:06 +000053 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.preservesCFG();
55 }
56
Chris Lattner260ab202002-04-18 17:39:14 +000057 // Visitation implementation - Implement instruction combining for different
58 // instruction types. The semantics are as follows:
59 // Return Value:
60 // null - No change was made
61 // I - Change was made, I is still valid
62 // otherwise - Change was made, replace I with returned instruction
63 //
Chris Lattner5d6bec52002-05-06 17:03:21 +000064 Instruction *visitNot(UnaryOperator *I);
Chris Lattner260ab202002-04-18 17:39:14 +000065 Instruction *visitAdd(BinaryOperator *I);
66 Instruction *visitSub(BinaryOperator *I);
67 Instruction *visitMul(BinaryOperator *I);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +000068 Instruction *visitDiv(BinaryOperator *I);
69 Instruction *visitRem(BinaryOperator *I);
70 Instruction *visitAnd(BinaryOperator *I);
71 Instruction *visitOr (BinaryOperator *I);
72 Instruction *visitXor(BinaryOperator *I);
73 Instruction *visitSetCondInst(BinaryOperator *I);
74 Instruction *visitShiftInst(Instruction *I);
Chris Lattner260ab202002-04-18 17:39:14 +000075 Instruction *visitCastInst(CastInst *CI);
Chris Lattnerbbbdd852002-05-06 18:06:38 +000076 Instruction *visitPHINode(PHINode *PN);
Chris Lattner48a44f72002-05-02 17:06:02 +000077 Instruction *visitGetElementPtrInst(GetElementPtrInst *GEP);
Chris Lattner260ab202002-04-18 17:39:14 +000078 Instruction *visitMemAccessInst(MemAccessInst *MAI);
79
80 // visitInstruction - Specify what to return for unhandled instructions...
81 Instruction *visitInstruction(Instruction *I) { return 0; }
82 };
83}
84
85
Chris Lattner5d6bec52002-05-06 17:03:21 +000086Instruction *InstCombiner::visitNot(UnaryOperator *I) {
87 if (I->use_empty()) return 0; // Don't fix dead instructions...
88
89 // not (not X) = X
90 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(0)))
91 if (Op->getOpcode() == Instruction::Not) {
92 AddUsesToWorkList(I); // Add all modified instrs to worklist
93 I->replaceAllUsesWith(Op->getOperand(0));
94 return I;
95 }
96 return 0;
97}
98
Chris Lattner260ab202002-04-18 17:39:14 +000099
100// Make sure that this instruction has a constant on the right hand side if it
101// has any constant arguments. If not, fix it an return true.
102//
103static bool SimplifyBinOp(BinaryOperator *I) {
Chris Lattnerca081252001-12-14 16:52:21 +0000104 if (isa<Constant>(I->getOperand(0)) && !isa<Constant>(I->getOperand(1)))
Chris Lattner31ba1292002-04-29 22:24:47 +0000105 return !I->swapOperands();
Chris Lattner260ab202002-04-18 17:39:14 +0000106 return false;
107}
Chris Lattnerca081252001-12-14 16:52:21 +0000108
Chris Lattner9fa53de2002-05-06 16:49:18 +0000109// dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
110// instruction if the LHS is a constant zero (which is the 'negate' form).
111//
112static inline Value *dyn_castNegInst(Value *V) {
113 Instruction *I = dyn_cast<Instruction>(V);
114 if (!I || I->getOpcode() != Instruction::Sub) return 0;
115
116 if (I->getOperand(0) == Constant::getNullValue(I->getType()))
117 return I->getOperand(1);
118 return 0;
119}
120
Chris Lattner260ab202002-04-18 17:39:14 +0000121Instruction *InstCombiner::visitAdd(BinaryOperator *I) {
122 if (I->use_empty()) return 0; // Don't fix dead add instructions...
123 bool Changed = SimplifyBinOp(I);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000124 Value *LHS = I->getOperand(0), *RHS = I->getOperand(1);
125
126 // Eliminate 'add int %X, 0'
127 if (I->getType()->isIntegral() &&
128 RHS == Constant::getNullValue(I->getType())) {
129 AddUsesToWorkList(I); // Add all modified instrs to worklist
130 I->replaceAllUsesWith(LHS);
131 return I;
132 }
133
Chris Lattner147e9752002-05-08 22:46:53 +0000134 // -A + B --> B - A
Chris Lattner9fa53de2002-05-06 16:49:18 +0000135 if (Value *V = dyn_castNegInst(LHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000136 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000137
138 // A + -B --> A - B
139 if (Value *V = dyn_castNegInst(RHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000140 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattner260ab202002-04-18 17:39:14 +0000141
142 // Simplify add instructions with a constant RHS...
Chris Lattner9fa53de2002-05-06 16:49:18 +0000143 if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
144 if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
145 if (ILHS->getOpcode() == Instruction::Add &&
146 isa<Constant>(ILHS->getOperand(1))) {
Chris Lattner260ab202002-04-18 17:39:14 +0000147 // Fold:
148 // %Y = add int %X, 1
149 // %Z = add int %Y, 1
150 // into:
151 // %Z = add int %X, 2
152 //
Chris Lattner9fa53de2002-05-06 16:49:18 +0000153 if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
154 I->setOperand(0, ILHS->getOperand(0));
Chris Lattner260ab202002-04-18 17:39:14 +0000155 I->setOperand(1, Val);
Chris Lattnerbee86622002-03-11 23:28:45 +0000156 return I;
Chris Lattner04805fa2002-02-26 21:46:54 +0000157 }
Chris Lattnerca081252001-12-14 16:52:21 +0000158 }
159 }
Chris Lattnerca081252001-12-14 16:52:21 +0000160 }
161
Chris Lattner260ab202002-04-18 17:39:14 +0000162 return Changed ? I : 0;
163}
164
165Instruction *InstCombiner::visitSub(BinaryOperator *I) {
166 if (I->use_empty()) return 0; // Don't fix dead add instructions...
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000167 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
168
169 if (Op0 == Op1) { // sub X, X -> 0
170 AddUsesToWorkList(I); // Add all modified instrs to worklist
171 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
172 return I;
173 }
Chris Lattner260ab202002-04-18 17:39:14 +0000174
175 // If this is a subtract instruction with a constant RHS, convert it to an add
176 // instruction of a negative constant
177 //
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000178 if (Constant *Op2 = dyn_cast<Constant>(Op1))
179 if (Constant *RHS = *Constant::getNullValue(I->getType()) - *Op2) // 0 - RHS
180 return BinaryOperator::create(Instruction::Add, Op0, RHS, I->getName());
Chris Lattner260ab202002-04-18 17:39:14 +0000181
Chris Lattner147e9752002-05-08 22:46:53 +0000182 // If this is a 'C = x-B', check to see if 'B = -A', so that C = x+A...
183 if (Value *V = dyn_castNegInst(Op1))
184 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000185
Chris Lattnerad3c4952002-05-09 01:29:19 +0000186 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
187 // not used by anyone else...
188 //
189 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
190 if (Op1I->use_size() == 1) {
191 // Swap the two operands of the subexpr...
192 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
193 Op1I->setOperand(0, IIOp1);
194 Op1I->setOperand(1, IIOp0);
195
196 // Create the new top level add instruction...
197 return BinaryOperator::create(Instruction::Add, Op0, Op1);
198 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000199 return 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000200}
201
202Instruction *InstCombiner::visitMul(BinaryOperator *I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000203 if (I->use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner260ab202002-04-18 17:39:14 +0000204 bool Changed = SimplifyBinOp(I);
205 Value *Op1 = I->getOperand(0);
206
207 // Simplify add instructions with a constant RHS...
208 if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(1))) {
209 if (I->getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){
210 // Eliminate 'mul int %X, 1'
211 AddUsesToWorkList(I); // Add all modified instrs to worklist
212 I->replaceAllUsesWith(Op1);
213 return I;
Chris Lattner31ba1292002-04-29 22:24:47 +0000214
215 } else if (I->getType()->isIntegral() &&
216 cast<ConstantInt>(Op2)->equalsInt(2)) {
217 // Convert 'mul int %X, 2' to 'add int %X, %X'
218 return BinaryOperator::create(Instruction::Add, Op1, Op1, I->getName());
219
220 } else if (Op2->isNullValue()) {
221 // Eliminate 'mul int %X, 0'
222 AddUsesToWorkList(I); // Add all modified instrs to worklist
223 I->replaceAllUsesWith(Op2); // Set this value to zero directly
224 return I;
Chris Lattner260ab202002-04-18 17:39:14 +0000225 }
226 }
227
228 return Changed ? I : 0;
229}
230
231
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000232Instruction *InstCombiner::visitDiv(BinaryOperator *I) {
233 if (I->use_empty()) return 0; // Don't fix dead instructions...
234
235 // div X, 1 == X
236 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1)))
237 if (RHS->equalsInt(1)) {
238 AddUsesToWorkList(I); // Add all modified instrs to worklist
239 I->replaceAllUsesWith(I->getOperand(0));
240 return I;
241 }
242 return 0;
243}
244
245
246Instruction *InstCombiner::visitRem(BinaryOperator *I) {
247 if (I->use_empty()) return 0; // Don't fix dead instructions...
248
249 // rem X, 1 == 0
250 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1)))
251 if (RHS->equalsInt(1)) {
252 AddUsesToWorkList(I); // Add all modified instrs to worklist
253 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
254 return I;
255 }
256 return 0;
257}
258
259static Constant *getMaxValue(const Type *Ty) {
260 assert(Ty == Type::BoolTy || Ty->isIntegral());
261 if (Ty == Type::BoolTy)
262 return ConstantBool::True;
263
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000264 if (Ty->isSigned())
Chris Lattnera6e047a2002-05-06 18:54:59 +0000265 return ConstantSInt::get(Ty, -1);
266 else if (Ty->isUnsigned()) {
267 // Calculate -1 casted to the right type...
268 unsigned TypeBits = Ty->getPrimitiveSize()*8;
269 uint64_t Val = (uint64_t)-1LL; // All ones
270 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000271 return ConstantUInt::get(Ty, Val);
Chris Lattnera6e047a2002-05-06 18:54:59 +0000272 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000273 return 0;
274}
275
276
277Instruction *InstCombiner::visitAnd(BinaryOperator *I) {
278 if (I->use_empty()) return 0; // Don't fix dead instructions...
279 bool Changed = SimplifyBinOp(I);
280 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
281
282 // and X, X = X and X, 0 == 0
283 if (Op0 == Op1 || Op1 == Constant::getNullValue(I->getType())) {
284 AddUsesToWorkList(I); // Add all modified instrs to worklist
285 I->replaceAllUsesWith(Op1);
286 return I;
287 }
288
289 // and X, -1 == X
290 if (Constant *RHS = dyn_cast<Constant>(Op1))
291 if (RHS == getMaxValue(I->getType())) {
292 AddUsesToWorkList(I); // Add all modified instrs to worklist
293 I->replaceAllUsesWith(Op0);
294 return I;
295 }
296
297 return Changed ? I : 0;
298}
299
300
301
302Instruction *InstCombiner::visitOr(BinaryOperator *I) {
303 if (I->use_empty()) return 0; // Don't fix dead instructions...
304 bool Changed = SimplifyBinOp(I);
305 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
306
307 // or X, X = X or X, 0 == X
308 if (Op0 == Op1 || Op1 == Constant::getNullValue(I->getType())) {
309 AddUsesToWorkList(I); // Add all modified instrs to worklist
310 I->replaceAllUsesWith(Op0);
311 return I;
312 }
313
314 // or X, -1 == -1
315 if (Constant *RHS = dyn_cast<Constant>(Op1))
316 if (RHS == getMaxValue(I->getType())) {
317 AddUsesToWorkList(I); // Add all modified instrs to worklist
318 I->replaceAllUsesWith(Op1);
319 return I;
320 }
321
322 return Changed ? I : 0;
323}
324
325
326
327Instruction *InstCombiner::visitXor(BinaryOperator *I) {
328 if (I->use_empty()) return 0; // Don't fix dead instructions...
329 bool Changed = SimplifyBinOp(I);
330 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
331
332 // xor X, X = 0
333 if (Op0 == Op1) {
334 AddUsesToWorkList(I); // Add all modified instrs to worklist
335 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
336 return I;
337 }
338
339 // xor X, 0 == X
340 if (Op1 == Constant::getNullValue(I->getType())) {
341 AddUsesToWorkList(I); // Add all modified instrs to worklist
342 I->replaceAllUsesWith(Op0);
343 return I;
344 }
345
346 return Changed ? I : 0;
347}
348
Chris Lattner1fc23f32002-05-09 20:11:54 +0000349// isTrueWhenEqual - Return true if the specified setcondinst instruction is
350// true when both operands are equal...
351//
352static bool isTrueWhenEqual(Instruction *I) {
353 return I->getOpcode() == Instruction::SetEQ ||
354 I->getOpcode() == Instruction::SetGE ||
355 I->getOpcode() == Instruction::SetLE;
356}
357
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000358Instruction *InstCombiner::visitSetCondInst(BinaryOperator *I) {
359 if (I->use_empty()) return 0; // Don't fix dead instructions...
360 bool Changed = SimplifyBinOp(I);
361
362 // setcc X, X
363 if (I->getOperand(0) == I->getOperand(1)) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000364 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner1fc23f32002-05-09 20:11:54 +0000365 I->replaceAllUsesWith(ConstantBool::get(isTrueWhenEqual(I)));
366 return I;
367 }
368
369 // setcc <global*>, 0 - Global value addresses are never null!
370 if (isa<GlobalValue>(I->getOperand(0)) &&
371 isa<ConstantPointerNull>(I->getOperand(1))) {
372 AddUsesToWorkList(I); // Add all modified instrs to worklist
373 I->replaceAllUsesWith(ConstantBool::get(!isTrueWhenEqual(I)));
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000374 return I;
375 }
376
377 return Changed ? I : 0;
378}
379
380
381
382Instruction *InstCombiner::visitShiftInst(Instruction *I) {
383 if (I->use_empty()) return 0; // Don't fix dead instructions...
384 assert(I->getOperand(1)->getType() == Type::UByteTy);
385 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
386
387 // shl X, 0 == X and shr X, 0 == X
388 // shl 0, X == 0 and shr 0, X == 0
389 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
390 Op0 == Constant::getNullValue(Op0->getType())) {
391 AddUsesToWorkList(I); // Add all modified instrs to worklist
392 I->replaceAllUsesWith(Op0);
393 return I;
394 }
395
396 // shl int X, 32 = 0 and shr sbyte Y, 9 = 0, ... just don't eliminate shr of
397 // a signed value.
398 //
399 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
400 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
401 if (CUI->getValue() >= TypeBits &&
402 !(Op0->getType()->isSigned() && I->getOpcode() == Instruction::Shr)) {
403 AddUsesToWorkList(I); // Add all modified instrs to worklist
404 I->replaceAllUsesWith(Constant::getNullValue(Op0->getType()));
405 return I;
406 }
407 }
408 return 0;
409}
410
411
Chris Lattner48a44f72002-05-02 17:06:02 +0000412// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
413// instruction.
414//
415static inline bool isEliminableCastOfCast(const CastInst *CI,
416 const CastInst *CSrc) {
417 assert(CI->getOperand(0) == CSrc);
418 const Type *SrcTy = CSrc->getOperand(0)->getType();
419 const Type *MidTy = CSrc->getType();
420 const Type *DstTy = CI->getType();
421
422 // It is legal to eliminate the instruction if casting A->B->A
423 if (SrcTy == DstTy) return true;
424
425 // Allow free casting and conversion of sizes as long as the sign doesn't
426 // change...
427 if (SrcTy->isSigned() == MidTy->isSigned() &&
428 MidTy->isSigned() == DstTy->isSigned())
429 return true;
430
431 // Otherwise, we cannot succeed. Specifically we do not want to allow things
432 // like: short -> ushort -> uint, because this can create wrong results if
433 // the input short is negative!
434 //
435 return false;
436}
437
438
439// CastInst simplification
Chris Lattner260ab202002-04-18 17:39:14 +0000440//
441Instruction *InstCombiner::visitCastInst(CastInst *CI) {
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000442 if (CI->use_empty()) return 0; // Don't fix dead instructions...
443
Chris Lattner48a44f72002-05-02 17:06:02 +0000444 // If the user is casting a value to the same type, eliminate this cast
445 // instruction...
Chris Lattner260ab202002-04-18 17:39:14 +0000446 if (CI->getType() == CI->getOperand(0)->getType() && !CI->use_empty()) {
447 AddUsesToWorkList(CI); // Add all modified instrs to worklist
448 CI->replaceAllUsesWith(CI->getOperand(0));
449 return CI;
450 }
Chris Lattner48a44f72002-05-02 17:06:02 +0000451
452
453 // If casting the result of another cast instruction, try to eliminate this
454 // one!
455 //
456 if (CastInst *CSrc = dyn_cast<CastInst>(CI->getOperand(0)))
457 if (isEliminableCastOfCast(CI, CSrc)) {
458 // This instruction now refers directly to the cast's src operand. This
459 // has a good chance of making CSrc dead.
460 CI->setOperand(0, CSrc->getOperand(0));
461 return CI;
462 }
463
Chris Lattner260ab202002-04-18 17:39:14 +0000464 return 0;
Chris Lattnerca081252001-12-14 16:52:21 +0000465}
466
Chris Lattner48a44f72002-05-02 17:06:02 +0000467
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000468// PHINode simplification
469//
470Instruction *InstCombiner::visitPHINode(PHINode *PN) {
471 if (PN->use_empty()) return 0; // Don't fix dead instructions...
472
473 // If the PHI node only has one incoming value, eliminate the PHI node...
474 if (PN->getNumIncomingValues() == 1) {
475 AddUsesToWorkList(PN); // Add all modified instrs to worklist
476 PN->replaceAllUsesWith(PN->getIncomingValue(0));
477 return PN;
478 }
479
480 return 0;
481}
482
Chris Lattner48a44f72002-05-02 17:06:02 +0000483
484Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst *GEP) {
485 // Is it getelementptr %P, uint 0
486 // If so, elminate the noop.
487 if (GEP->getNumOperands() == 2 && !GEP->use_empty() &&
488 GEP->getOperand(1) == Constant::getNullValue(Type::UIntTy)) {
489 AddUsesToWorkList(GEP); // Add all modified instrs to worklist
490 GEP->replaceAllUsesWith(GEP->getOperand(0));
491 return GEP;
492 }
493
494 return visitMemAccessInst(GEP);
495}
496
497
Chris Lattnerca081252001-12-14 16:52:21 +0000498// Combine Indices - If the source pointer to this mem access instruction is a
499// getelementptr instruction, combine the indices of the GEP into this
500// instruction
501//
Chris Lattner260ab202002-04-18 17:39:14 +0000502Instruction *InstCombiner::visitMemAccessInst(MemAccessInst *MAI) {
Chris Lattnerca081252001-12-14 16:52:21 +0000503 GetElementPtrInst *Src =
504 dyn_cast<GetElementPtrInst>(MAI->getPointerOperand());
505 if (!Src) return 0;
506
Chris Lattner7f74a562002-01-20 22:54:45 +0000507 std::vector<Value *> Indices;
Chris Lattnerca081252001-12-14 16:52:21 +0000508
509 // Only special case we have to watch out for is pointer arithmetic on the
510 // 0th index of MAI.
511 unsigned FirstIdx = MAI->getFirstIndexOperandNumber();
512 if (FirstIdx == MAI->getNumOperands() ||
513 (FirstIdx == MAI->getNumOperands()-1 &&
514 MAI->getOperand(FirstIdx) == ConstantUInt::get(Type::UIntTy, 0))) {
515 // Replace the index list on this MAI with the index on the getelementptr
516 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
517 } else if (*MAI->idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) {
518 // Otherwise we can do the fold if the first index of the GEP is a zero
519 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
520 Indices.insert(Indices.end(), MAI->idx_begin()+1, MAI->idx_end());
521 }
522
523 if (Indices.empty()) return 0; // Can't do the fold?
524
525 switch (MAI->getOpcode()) {
526 case Instruction::GetElementPtr:
527 return new GetElementPtrInst(Src->getOperand(0), Indices, MAI->getName());
528 case Instruction::Load:
529 return new LoadInst(Src->getOperand(0), Indices, MAI->getName());
530 case Instruction::Store:
Chris Lattnerf40379e2002-04-18 14:43:54 +0000531 return new StoreInst(MAI->getOperand(0), Src->getOperand(0), Indices);
Chris Lattnerca081252001-12-14 16:52:21 +0000532 default:
533 assert(0 && "Unknown memaccessinst!");
534 break;
535 }
536 abort();
537 return 0;
538}
539
Chris Lattnerca081252001-12-14 16:52:21 +0000540
Chris Lattnerc8e66542002-04-27 06:56:12 +0000541bool InstCombiner::runOnFunction(Function *F) {
Chris Lattner260ab202002-04-18 17:39:14 +0000542 bool Changed = false;
Chris Lattnerca081252001-12-14 16:52:21 +0000543
Chris Lattner260ab202002-04-18 17:39:14 +0000544 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattnerca081252001-12-14 16:52:21 +0000545
546 while (!WorkList.empty()) {
547 Instruction *I = WorkList.back(); // Get an instruction from the worklist
548 WorkList.pop_back();
549
550 // Now that we have an instruction, try combining it to simplify it...
Chris Lattner260ab202002-04-18 17:39:14 +0000551 Instruction *Result = visit(I);
552 if (Result) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000553 ++NumCombined;
Chris Lattner260ab202002-04-18 17:39:14 +0000554 // Should we replace the old instruction with a new one?
Chris Lattner053c0932002-05-14 15:24:07 +0000555 if (Result != I) {
556 // Instructions can end up on the worklist more than once. Make sure
557 // we do not process an instruction that has been deleted.
558 std::vector<Instruction*>::iterator It = std::find(WorkList.begin(),
559 WorkList.end(), I);
560 while (It != WorkList.end()) {
561 It = WorkList.erase(It);
562 It = std::find(It, WorkList.end(), I);
563 }
564
Chris Lattner260ab202002-04-18 17:39:14 +0000565 ReplaceInstWithInst(I, Result);
Chris Lattner053c0932002-05-14 15:24:07 +0000566 }
Chris Lattner260ab202002-04-18 17:39:14 +0000567
568 WorkList.push_back(Result);
569 AddUsesToWorkList(Result);
570 Changed = true;
Chris Lattnerca081252001-12-14 16:52:21 +0000571 }
572 }
573
Chris Lattner260ab202002-04-18 17:39:14 +0000574 return Changed;
Chris Lattner04805fa2002-02-26 21:46:54 +0000575}
576
577Pass *createInstructionCombiningPass() {
Chris Lattner260ab202002-04-18 17:39:14 +0000578 return new InstCombiner();
Chris Lattner04805fa2002-02-26 21:46:54 +0000579}