blob: 0f676426e1ba3cecdbbdb8579d6a978c1a26fc42 [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 Lattnerca081252001-12-14 16:52:21 +000029
Chris Lattner0b18c1d2002-05-10 15:38:35 +000030static Statistic<> NumCombined("instcombine\t- Number of insts combined");
Chris Lattnerca081252001-12-14 16:52:21 +000031
Chris Lattner260ab202002-04-18 17:39:14 +000032namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +000033 class InstCombiner : public FunctionPass,
Chris Lattner260ab202002-04-18 17:39:14 +000034 public InstVisitor<InstCombiner, Instruction*> {
35 // Worklist of all of the instructions that need to be simplified.
36 std::vector<Instruction*> WorkList;
37
38 void AddUsesToWorkList(Instruction *I) {
39 // The instruction was simplified, add all users of the instruction to
40 // the work lists because they might get more simplified now...
41 //
42 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
43 UI != UE; ++UI)
44 WorkList.push_back(cast<Instruction>(*UI));
45 }
46
47 public:
Chris Lattner37104aa2002-04-29 14:57:45 +000048 const char *getPassName() const { return "Instruction Combining"; }
49
Chris Lattnerc8e66542002-04-27 06:56:12 +000050 virtual bool runOnFunction(Function *F);
Chris Lattner260ab202002-04-18 17:39:14 +000051
Chris Lattnerf12cc842002-04-28 21:27:06 +000052 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.preservesCFG();
54 }
55
Chris Lattner260ab202002-04-18 17:39:14 +000056 // Visitation implementation - Implement instruction combining for different
57 // instruction types. The semantics are as follows:
58 // Return Value:
59 // null - No change was made
60 // I - Change was made, I is still valid
61 // otherwise - Change was made, replace I with returned instruction
62 //
Chris Lattner5d6bec52002-05-06 17:03:21 +000063 Instruction *visitNot(UnaryOperator *I);
Chris Lattner260ab202002-04-18 17:39:14 +000064 Instruction *visitAdd(BinaryOperator *I);
65 Instruction *visitSub(BinaryOperator *I);
66 Instruction *visitMul(BinaryOperator *I);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +000067 Instruction *visitDiv(BinaryOperator *I);
68 Instruction *visitRem(BinaryOperator *I);
69 Instruction *visitAnd(BinaryOperator *I);
70 Instruction *visitOr (BinaryOperator *I);
71 Instruction *visitXor(BinaryOperator *I);
72 Instruction *visitSetCondInst(BinaryOperator *I);
73 Instruction *visitShiftInst(Instruction *I);
Chris Lattner260ab202002-04-18 17:39:14 +000074 Instruction *visitCastInst(CastInst *CI);
Chris Lattnerbbbdd852002-05-06 18:06:38 +000075 Instruction *visitPHINode(PHINode *PN);
Chris Lattner48a44f72002-05-02 17:06:02 +000076 Instruction *visitGetElementPtrInst(GetElementPtrInst *GEP);
Chris Lattner260ab202002-04-18 17:39:14 +000077 Instruction *visitMemAccessInst(MemAccessInst *MAI);
78
79 // visitInstruction - Specify what to return for unhandled instructions...
80 Instruction *visitInstruction(Instruction *I) { return 0; }
81 };
82}
83
84
Chris Lattner5d6bec52002-05-06 17:03:21 +000085Instruction *InstCombiner::visitNot(UnaryOperator *I) {
86 if (I->use_empty()) return 0; // Don't fix dead instructions...
87
88 // not (not X) = X
89 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(0)))
90 if (Op->getOpcode() == Instruction::Not) {
91 AddUsesToWorkList(I); // Add all modified instrs to worklist
92 I->replaceAllUsesWith(Op->getOperand(0));
93 return I;
94 }
95 return 0;
96}
97
Chris Lattner260ab202002-04-18 17:39:14 +000098
99// Make sure that this instruction has a constant on the right hand side if it
100// has any constant arguments. If not, fix it an return true.
101//
102static bool SimplifyBinOp(BinaryOperator *I) {
Chris Lattnerca081252001-12-14 16:52:21 +0000103 if (isa<Constant>(I->getOperand(0)) && !isa<Constant>(I->getOperand(1)))
Chris Lattner31ba1292002-04-29 22:24:47 +0000104 return !I->swapOperands();
Chris Lattner260ab202002-04-18 17:39:14 +0000105 return false;
106}
Chris Lattnerca081252001-12-14 16:52:21 +0000107
Chris Lattner9fa53de2002-05-06 16:49:18 +0000108// dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
109// instruction if the LHS is a constant zero (which is the 'negate' form).
110//
111static inline Value *dyn_castNegInst(Value *V) {
112 Instruction *I = dyn_cast<Instruction>(V);
113 if (!I || I->getOpcode() != Instruction::Sub) return 0;
114
115 if (I->getOperand(0) == Constant::getNullValue(I->getType()))
116 return I->getOperand(1);
117 return 0;
118}
119
Chris Lattner260ab202002-04-18 17:39:14 +0000120Instruction *InstCombiner::visitAdd(BinaryOperator *I) {
121 if (I->use_empty()) return 0; // Don't fix dead add instructions...
122 bool Changed = SimplifyBinOp(I);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000123 Value *LHS = I->getOperand(0), *RHS = I->getOperand(1);
124
125 // Eliminate 'add int %X, 0'
126 if (I->getType()->isIntegral() &&
127 RHS == Constant::getNullValue(I->getType())) {
128 AddUsesToWorkList(I); // Add all modified instrs to worklist
129 I->replaceAllUsesWith(LHS);
130 return I;
131 }
132
Chris Lattner147e9752002-05-08 22:46:53 +0000133 // -A + B --> B - A
Chris Lattner9fa53de2002-05-06 16:49:18 +0000134 if (Value *V = dyn_castNegInst(LHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000135 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000136
137 // A + -B --> A - B
138 if (Value *V = dyn_castNegInst(RHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000139 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattner260ab202002-04-18 17:39:14 +0000140
141 // Simplify add instructions with a constant RHS...
Chris Lattner9fa53de2002-05-06 16:49:18 +0000142 if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
143 if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
144 if (ILHS->getOpcode() == Instruction::Add &&
145 isa<Constant>(ILHS->getOperand(1))) {
Chris Lattner260ab202002-04-18 17:39:14 +0000146 // Fold:
147 // %Y = add int %X, 1
148 // %Z = add int %Y, 1
149 // into:
150 // %Z = add int %X, 2
151 //
Chris Lattner9fa53de2002-05-06 16:49:18 +0000152 if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
153 I->setOperand(0, ILHS->getOperand(0));
Chris Lattner260ab202002-04-18 17:39:14 +0000154 I->setOperand(1, Val);
Chris Lattnerbee86622002-03-11 23:28:45 +0000155 return I;
Chris Lattner04805fa2002-02-26 21:46:54 +0000156 }
Chris Lattnerca081252001-12-14 16:52:21 +0000157 }
158 }
Chris Lattnerca081252001-12-14 16:52:21 +0000159 }
160
Chris Lattner260ab202002-04-18 17:39:14 +0000161 return Changed ? I : 0;
162}
163
164Instruction *InstCombiner::visitSub(BinaryOperator *I) {
165 if (I->use_empty()) return 0; // Don't fix dead add instructions...
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000166 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
167
168 if (Op0 == Op1) { // sub X, X -> 0
169 AddUsesToWorkList(I); // Add all modified instrs to worklist
170 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
171 return I;
172 }
Chris Lattner260ab202002-04-18 17:39:14 +0000173
174 // If this is a subtract instruction with a constant RHS, convert it to an add
175 // instruction of a negative constant
176 //
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000177 if (Constant *Op2 = dyn_cast<Constant>(Op1))
178 if (Constant *RHS = *Constant::getNullValue(I->getType()) - *Op2) // 0 - RHS
179 return BinaryOperator::create(Instruction::Add, Op0, RHS, I->getName());
Chris Lattner260ab202002-04-18 17:39:14 +0000180
Chris Lattner147e9752002-05-08 22:46:53 +0000181 // If this is a 'C = x-B', check to see if 'B = -A', so that C = x+A...
182 if (Value *V = dyn_castNegInst(Op1))
183 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000184
Chris Lattnerad3c4952002-05-09 01:29:19 +0000185 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
186 // not used by anyone else...
187 //
188 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
189 if (Op1I->use_size() == 1) {
190 // Swap the two operands of the subexpr...
191 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
192 Op1I->setOperand(0, IIOp1);
193 Op1I->setOperand(1, IIOp0);
194
195 // Create the new top level add instruction...
196 return BinaryOperator::create(Instruction::Add, Op0, Op1);
197 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000198 return 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000199}
200
201Instruction *InstCombiner::visitMul(BinaryOperator *I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000202 if (I->use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner260ab202002-04-18 17:39:14 +0000203 bool Changed = SimplifyBinOp(I);
204 Value *Op1 = I->getOperand(0);
205
206 // Simplify add instructions with a constant RHS...
207 if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(1))) {
208 if (I->getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){
209 // Eliminate 'mul int %X, 1'
210 AddUsesToWorkList(I); // Add all modified instrs to worklist
211 I->replaceAllUsesWith(Op1);
212 return I;
Chris Lattner31ba1292002-04-29 22:24:47 +0000213
214 } else if (I->getType()->isIntegral() &&
215 cast<ConstantInt>(Op2)->equalsInt(2)) {
216 // Convert 'mul int %X, 2' to 'add int %X, %X'
217 return BinaryOperator::create(Instruction::Add, Op1, Op1, I->getName());
218
219 } else if (Op2->isNullValue()) {
220 // Eliminate 'mul int %X, 0'
221 AddUsesToWorkList(I); // Add all modified instrs to worklist
222 I->replaceAllUsesWith(Op2); // Set this value to zero directly
223 return I;
Chris Lattner260ab202002-04-18 17:39:14 +0000224 }
225 }
226
227 return Changed ? I : 0;
228}
229
230
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000231Instruction *InstCombiner::visitDiv(BinaryOperator *I) {
232 if (I->use_empty()) return 0; // Don't fix dead instructions...
233
234 // div X, 1 == X
235 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1)))
236 if (RHS->equalsInt(1)) {
237 AddUsesToWorkList(I); // Add all modified instrs to worklist
238 I->replaceAllUsesWith(I->getOperand(0));
239 return I;
240 }
241 return 0;
242}
243
244
245Instruction *InstCombiner::visitRem(BinaryOperator *I) {
246 if (I->use_empty()) return 0; // Don't fix dead instructions...
247
248 // rem X, 1 == 0
249 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1)))
250 if (RHS->equalsInt(1)) {
251 AddUsesToWorkList(I); // Add all modified instrs to worklist
252 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
253 return I;
254 }
255 return 0;
256}
257
258static Constant *getMaxValue(const Type *Ty) {
259 assert(Ty == Type::BoolTy || Ty->isIntegral());
260 if (Ty == Type::BoolTy)
261 return ConstantBool::True;
262
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000263 if (Ty->isSigned())
Chris Lattnera6e047a2002-05-06 18:54:59 +0000264 return ConstantSInt::get(Ty, -1);
265 else if (Ty->isUnsigned()) {
266 // Calculate -1 casted to the right type...
267 unsigned TypeBits = Ty->getPrimitiveSize()*8;
268 uint64_t Val = (uint64_t)-1LL; // All ones
269 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000270 return ConstantUInt::get(Ty, Val);
Chris Lattnera6e047a2002-05-06 18:54:59 +0000271 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000272 return 0;
273}
274
275
276Instruction *InstCombiner::visitAnd(BinaryOperator *I) {
277 if (I->use_empty()) return 0; // Don't fix dead instructions...
278 bool Changed = SimplifyBinOp(I);
279 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
280
281 // and X, X = X and X, 0 == 0
282 if (Op0 == Op1 || Op1 == Constant::getNullValue(I->getType())) {
283 AddUsesToWorkList(I); // Add all modified instrs to worklist
284 I->replaceAllUsesWith(Op1);
285 return I;
286 }
287
288 // and X, -1 == X
289 if (Constant *RHS = dyn_cast<Constant>(Op1))
290 if (RHS == getMaxValue(I->getType())) {
291 AddUsesToWorkList(I); // Add all modified instrs to worklist
292 I->replaceAllUsesWith(Op0);
293 return I;
294 }
295
296 return Changed ? I : 0;
297}
298
299
300
301Instruction *InstCombiner::visitOr(BinaryOperator *I) {
302 if (I->use_empty()) return 0; // Don't fix dead instructions...
303 bool Changed = SimplifyBinOp(I);
304 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
305
306 // or X, X = X or X, 0 == X
307 if (Op0 == Op1 || Op1 == Constant::getNullValue(I->getType())) {
308 AddUsesToWorkList(I); // Add all modified instrs to worklist
309 I->replaceAllUsesWith(Op0);
310 return I;
311 }
312
313 // or X, -1 == -1
314 if (Constant *RHS = dyn_cast<Constant>(Op1))
315 if (RHS == getMaxValue(I->getType())) {
316 AddUsesToWorkList(I); // Add all modified instrs to worklist
317 I->replaceAllUsesWith(Op1);
318 return I;
319 }
320
321 return Changed ? I : 0;
322}
323
324
325
326Instruction *InstCombiner::visitXor(BinaryOperator *I) {
327 if (I->use_empty()) return 0; // Don't fix dead instructions...
328 bool Changed = SimplifyBinOp(I);
329 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
330
331 // xor X, X = 0
332 if (Op0 == Op1) {
333 AddUsesToWorkList(I); // Add all modified instrs to worklist
334 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
335 return I;
336 }
337
338 // xor X, 0 == X
339 if (Op1 == Constant::getNullValue(I->getType())) {
340 AddUsesToWorkList(I); // Add all modified instrs to worklist
341 I->replaceAllUsesWith(Op0);
342 return I;
343 }
344
345 return Changed ? I : 0;
346}
347
Chris Lattner1fc23f32002-05-09 20:11:54 +0000348// isTrueWhenEqual - Return true if the specified setcondinst instruction is
349// true when both operands are equal...
350//
351static bool isTrueWhenEqual(Instruction *I) {
352 return I->getOpcode() == Instruction::SetEQ ||
353 I->getOpcode() == Instruction::SetGE ||
354 I->getOpcode() == Instruction::SetLE;
355}
356
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000357Instruction *InstCombiner::visitSetCondInst(BinaryOperator *I) {
358 if (I->use_empty()) return 0; // Don't fix dead instructions...
359 bool Changed = SimplifyBinOp(I);
360
361 // setcc X, X
362 if (I->getOperand(0) == I->getOperand(1)) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000363 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner1fc23f32002-05-09 20:11:54 +0000364 I->replaceAllUsesWith(ConstantBool::get(isTrueWhenEqual(I)));
365 return I;
366 }
367
368 // setcc <global*>, 0 - Global value addresses are never null!
369 if (isa<GlobalValue>(I->getOperand(0)) &&
370 isa<ConstantPointerNull>(I->getOperand(1))) {
371 AddUsesToWorkList(I); // Add all modified instrs to worklist
372 I->replaceAllUsesWith(ConstantBool::get(!isTrueWhenEqual(I)));
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000373 return I;
374 }
375
376 return Changed ? I : 0;
377}
378
379
380
381Instruction *InstCombiner::visitShiftInst(Instruction *I) {
382 if (I->use_empty()) return 0; // Don't fix dead instructions...
383 assert(I->getOperand(1)->getType() == Type::UByteTy);
384 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
385
386 // shl X, 0 == X and shr X, 0 == X
387 // shl 0, X == 0 and shr 0, X == 0
388 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
389 Op0 == Constant::getNullValue(Op0->getType())) {
390 AddUsesToWorkList(I); // Add all modified instrs to worklist
391 I->replaceAllUsesWith(Op0);
392 return I;
393 }
394
395 // shl int X, 32 = 0 and shr sbyte Y, 9 = 0, ... just don't eliminate shr of
396 // a signed value.
397 //
398 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
399 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
400 if (CUI->getValue() >= TypeBits &&
401 !(Op0->getType()->isSigned() && I->getOpcode() == Instruction::Shr)) {
402 AddUsesToWorkList(I); // Add all modified instrs to worklist
403 I->replaceAllUsesWith(Constant::getNullValue(Op0->getType()));
404 return I;
405 }
406 }
407 return 0;
408}
409
410
Chris Lattner48a44f72002-05-02 17:06:02 +0000411// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
412// instruction.
413//
414static inline bool isEliminableCastOfCast(const CastInst *CI,
415 const CastInst *CSrc) {
416 assert(CI->getOperand(0) == CSrc);
417 const Type *SrcTy = CSrc->getOperand(0)->getType();
418 const Type *MidTy = CSrc->getType();
419 const Type *DstTy = CI->getType();
420
421 // It is legal to eliminate the instruction if casting A->B->A
422 if (SrcTy == DstTy) return true;
423
424 // Allow free casting and conversion of sizes as long as the sign doesn't
425 // change...
426 if (SrcTy->isSigned() == MidTy->isSigned() &&
427 MidTy->isSigned() == DstTy->isSigned())
428 return true;
429
430 // Otherwise, we cannot succeed. Specifically we do not want to allow things
431 // like: short -> ushort -> uint, because this can create wrong results if
432 // the input short is negative!
433 //
434 return false;
435}
436
437
438// CastInst simplification
Chris Lattner260ab202002-04-18 17:39:14 +0000439//
440Instruction *InstCombiner::visitCastInst(CastInst *CI) {
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000441 if (CI->use_empty()) return 0; // Don't fix dead instructions...
442
Chris Lattner48a44f72002-05-02 17:06:02 +0000443 // If the user is casting a value to the same type, eliminate this cast
444 // instruction...
Chris Lattner260ab202002-04-18 17:39:14 +0000445 if (CI->getType() == CI->getOperand(0)->getType() && !CI->use_empty()) {
446 AddUsesToWorkList(CI); // Add all modified instrs to worklist
447 CI->replaceAllUsesWith(CI->getOperand(0));
448 return CI;
449 }
Chris Lattner48a44f72002-05-02 17:06:02 +0000450
451
452 // If casting the result of another cast instruction, try to eliminate this
453 // one!
454 //
455 if (CastInst *CSrc = dyn_cast<CastInst>(CI->getOperand(0)))
456 if (isEliminableCastOfCast(CI, CSrc)) {
457 // This instruction now refers directly to the cast's src operand. This
458 // has a good chance of making CSrc dead.
459 CI->setOperand(0, CSrc->getOperand(0));
460 return CI;
461 }
462
Chris Lattner260ab202002-04-18 17:39:14 +0000463 return 0;
Chris Lattnerca081252001-12-14 16:52:21 +0000464}
465
Chris Lattner48a44f72002-05-02 17:06:02 +0000466
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000467// PHINode simplification
468//
469Instruction *InstCombiner::visitPHINode(PHINode *PN) {
470 if (PN->use_empty()) return 0; // Don't fix dead instructions...
471
472 // If the PHI node only has one incoming value, eliminate the PHI node...
473 if (PN->getNumIncomingValues() == 1) {
474 AddUsesToWorkList(PN); // Add all modified instrs to worklist
475 PN->replaceAllUsesWith(PN->getIncomingValue(0));
476 return PN;
477 }
478
479 return 0;
480}
481
Chris Lattner48a44f72002-05-02 17:06:02 +0000482
483Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst *GEP) {
484 // Is it getelementptr %P, uint 0
485 // If so, elminate the noop.
486 if (GEP->getNumOperands() == 2 && !GEP->use_empty() &&
487 GEP->getOperand(1) == Constant::getNullValue(Type::UIntTy)) {
488 AddUsesToWorkList(GEP); // Add all modified instrs to worklist
489 GEP->replaceAllUsesWith(GEP->getOperand(0));
490 return GEP;
491 }
492
493 return visitMemAccessInst(GEP);
494}
495
496
Chris Lattnerca081252001-12-14 16:52:21 +0000497// Combine Indices - If the source pointer to this mem access instruction is a
498// getelementptr instruction, combine the indices of the GEP into this
499// instruction
500//
Chris Lattner260ab202002-04-18 17:39:14 +0000501Instruction *InstCombiner::visitMemAccessInst(MemAccessInst *MAI) {
Chris Lattnerca081252001-12-14 16:52:21 +0000502 GetElementPtrInst *Src =
503 dyn_cast<GetElementPtrInst>(MAI->getPointerOperand());
504 if (!Src) return 0;
505
Chris Lattner7f74a562002-01-20 22:54:45 +0000506 std::vector<Value *> Indices;
Chris Lattnerca081252001-12-14 16:52:21 +0000507
508 // Only special case we have to watch out for is pointer arithmetic on the
509 // 0th index of MAI.
510 unsigned FirstIdx = MAI->getFirstIndexOperandNumber();
511 if (FirstIdx == MAI->getNumOperands() ||
512 (FirstIdx == MAI->getNumOperands()-1 &&
513 MAI->getOperand(FirstIdx) == ConstantUInt::get(Type::UIntTy, 0))) {
514 // Replace the index list on this MAI with the index on the getelementptr
515 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
516 } else if (*MAI->idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) {
517 // Otherwise we can do the fold if the first index of the GEP is a zero
518 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
519 Indices.insert(Indices.end(), MAI->idx_begin()+1, MAI->idx_end());
520 }
521
522 if (Indices.empty()) return 0; // Can't do the fold?
523
524 switch (MAI->getOpcode()) {
525 case Instruction::GetElementPtr:
526 return new GetElementPtrInst(Src->getOperand(0), Indices, MAI->getName());
527 case Instruction::Load:
528 return new LoadInst(Src->getOperand(0), Indices, MAI->getName());
529 case Instruction::Store:
Chris Lattnerf40379e2002-04-18 14:43:54 +0000530 return new StoreInst(MAI->getOperand(0), Src->getOperand(0), Indices);
Chris Lattnerca081252001-12-14 16:52:21 +0000531 default:
532 assert(0 && "Unknown memaccessinst!");
533 break;
534 }
535 abort();
536 return 0;
537}
538
Chris Lattnerca081252001-12-14 16:52:21 +0000539
Chris Lattnerc8e66542002-04-27 06:56:12 +0000540bool InstCombiner::runOnFunction(Function *F) {
Chris Lattner260ab202002-04-18 17:39:14 +0000541 bool Changed = false;
Chris Lattnerca081252001-12-14 16:52:21 +0000542
Chris Lattner260ab202002-04-18 17:39:14 +0000543 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattnerca081252001-12-14 16:52:21 +0000544
545 while (!WorkList.empty()) {
546 Instruction *I = WorkList.back(); // Get an instruction from the worklist
547 WorkList.pop_back();
548
549 // Now that we have an instruction, try combining it to simplify it...
Chris Lattner260ab202002-04-18 17:39:14 +0000550 Instruction *Result = visit(I);
551 if (Result) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000552 ++NumCombined;
Chris Lattner260ab202002-04-18 17:39:14 +0000553 // Should we replace the old instruction with a new one?
554 if (Result != I)
555 ReplaceInstWithInst(I, Result);
556
557 WorkList.push_back(Result);
558 AddUsesToWorkList(Result);
559 Changed = true;
Chris Lattnerca081252001-12-14 16:52:21 +0000560 }
561 }
562
Chris Lattner260ab202002-04-18 17:39:14 +0000563 return Changed;
Chris Lattner04805fa2002-02-26 21:46:54 +0000564}
565
566Pass *createInstructionCombiningPass() {
Chris Lattner260ab202002-04-18 17:39:14 +0000567 return new InstCombiner();
Chris Lattner04805fa2002-02-26 21:46:54 +0000568}