Fix bug: test/Regression/Transforms/InstCombine/2002-08-02-CastTest.ll
[oota-llvm.git] / lib / Transforms / Scalar / InstructionCombining.cpp
1 //===- 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
5 //   make instructions dead, so a subsequent DIE pass is useful.  This pass is
6 //   where algebraic simplification happens.
7 //
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
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
20 #include "llvm/Transforms/Utils/Local.h"
21 #include "llvm/ConstantHandling.h"
22 #include "llvm/iMemory.h"
23 #include "llvm/iOther.h"
24 #include "llvm/iPHINode.h"
25 #include "llvm/iOperators.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/InstIterator.h"
28 #include "llvm/Support/InstVisitor.h"
29 #include "Support/StatisticReporter.h"
30 #include <algorithm>
31
32 static Statistic<> NumCombined("instcombine\t- Number of insts combined");
33
34 namespace {
35   class InstCombiner : public FunctionPass,
36                        public InstVisitor<InstCombiner, Instruction*> {
37     // Worklist of all of the instructions that need to be simplified.
38     std::vector<Instruction*> WorkList;
39
40     void AddUsesToWorkList(Instruction &I) {
41       // The instruction was simplified, add all users of the instruction to
42       // the work lists because they might get more simplified now...
43       //
44       for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
45            UI != UE; ++UI)
46         WorkList.push_back(cast<Instruction>(*UI));
47     }
48
49   public:
50     virtual bool runOnFunction(Function &F);
51
52     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53       AU.preservesCFG();
54     }
55
56     // 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     //   
63     Instruction *visitNot(UnaryOperator &I);
64     Instruction *visitAdd(BinaryOperator &I);
65     Instruction *visitSub(BinaryOperator &I);
66     Instruction *visitMul(BinaryOperator &I);
67     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);
74     Instruction *visitCastInst(CastInst &CI);
75     Instruction *visitPHINode(PHINode &PN);
76     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
77
78     // visitInstruction - Specify what to return for unhandled instructions...
79     Instruction *visitInstruction(Instruction &I) { return 0; }
80   };
81
82   RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
83 }
84
85
86 Instruction *InstCombiner::visitNot(UnaryOperator &I) {
87   // not (not X) = X
88   if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(0)))
89     if (Op->getOpcode() == Instruction::Not) {
90       AddUsesToWorkList(I);         // Add all modified instrs to worklist
91       I.replaceAllUsesWith(Op->getOperand(0));
92       return &I;
93     }
94   return 0;
95 }
96
97
98 // Make sure that this instruction has a constant on the right hand side if it
99 // has any constant arguments.  If not, fix it an return true.
100 //
101 static bool SimplifyBinOp(BinaryOperator &I) {
102   if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1)))
103     return !I.swapOperands();
104   return false;
105 }
106
107 // dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
108 // instruction if the LHS is a constant zero (which is the 'negate' form).
109 //
110 static inline Value *dyn_castNegInst(Value *V) {
111   Instruction *I = dyn_cast<Instruction>(V);
112   if (!I || I->getOpcode() != Instruction::Sub) return 0;
113
114   if (I->getOperand(0) == Constant::getNullValue(I->getType()))
115     return I->getOperand(1);
116   return 0;
117 }
118
119 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
120   bool Changed = SimplifyBinOp(I);
121   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
122
123   // Eliminate 'add int %X, 0'
124   if (RHS == Constant::getNullValue(I.getType())) {
125     AddUsesToWorkList(I);         // Add all modified instrs to worklist
126     I.replaceAllUsesWith(LHS);
127     return &I;
128   }
129
130   // -A + B  -->  B - A
131   if (Value *V = dyn_castNegInst(LHS))
132     return BinaryOperator::create(Instruction::Sub, RHS, V);
133
134   // A + -B  -->  A - B
135   if (Value *V = dyn_castNegInst(RHS))
136     return BinaryOperator::create(Instruction::Sub, LHS, V);
137
138   // Simplify add instructions with a constant RHS...
139   if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
140     if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
141       if (ILHS->getOpcode() == Instruction::Add &&
142           isa<Constant>(ILHS->getOperand(1))) {
143         // Fold:
144         //    %Y = add int %X, 1
145         //    %Z = add int %Y, 1
146         // into:
147         //    %Z = add int %X, 2
148         //
149         if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
150           I.setOperand(0, ILHS->getOperand(0));
151           I.setOperand(1, Val);
152           return &I;
153         }
154       }
155     }
156   }
157
158   return Changed ? &I : 0;
159 }
160
161 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
162   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
163
164   if (Op0 == Op1) {         // sub X, X  -> 0
165     AddUsesToWorkList(I);         // Add all modified instrs to worklist
166     I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
167     return &I;
168   }
169
170   // If this is a subtract instruction with a constant RHS, convert it to an add
171   // instruction of a negative constant
172   //
173   if (Constant *Op2 = dyn_cast<Constant>(Op1))
174     if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS
175       return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName());
176
177   // If this is a 'C = x-B', check to see if 'B = -A', so that C = x+A...
178   if (Value *V = dyn_castNegInst(Op1))
179     return BinaryOperator::create(Instruction::Add, Op0, V);
180
181   // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
182   // not used by anyone else...
183   //
184   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
185     if (Op1I->use_size() == 1 && Op1I->getOpcode() == Instruction::Sub) {
186       // Swap the two operands of the subexpr...
187       Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
188       Op1I->setOperand(0, IIOp1);
189       Op1I->setOperand(1, IIOp0);
190
191       // Create the new top level add instruction...
192       return BinaryOperator::create(Instruction::Add, Op0, Op1);
193     }
194   return 0;
195 }
196
197 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
198   bool Changed = SimplifyBinOp(I);
199   Value *Op1 = I.getOperand(0);
200
201   // Simplify add instructions with a constant RHS...
202   if (Constant *Op2 = dyn_cast<Constant>(I.getOperand(1))) {
203     if (I.getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){
204       // Eliminate 'mul int %X, 1'
205       AddUsesToWorkList(I);         // Add all modified instrs to worklist
206       I.replaceAllUsesWith(Op1);
207       return &I;
208
209     } else if (I.getType()->isIntegral() &&
210                cast<ConstantInt>(Op2)->equalsInt(2)) {
211       // Convert 'mul int %X, 2' to 'add int %X, %X'
212       return BinaryOperator::create(Instruction::Add, Op1, Op1, I.getName());
213
214     } else if (Op2->isNullValue()) {
215       // Eliminate 'mul int %X, 0'
216       AddUsesToWorkList(I);        // Add all modified instrs to worklist
217       I.replaceAllUsesWith(Op2);   // Set this value to zero directly
218       return &I;
219     }
220   }
221
222   return Changed ? &I : 0;
223 }
224
225
226 Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
227   // div X, 1 == X
228   if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
229     if (RHS->equalsInt(1)) {
230       AddUsesToWorkList(I);         // Add all modified instrs to worklist
231       I.replaceAllUsesWith(I.getOperand(0));
232       return &I;
233     }
234   return 0;
235 }
236
237
238 Instruction *InstCombiner::visitRem(BinaryOperator &I) {
239   // rem X, 1 == 0
240   if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
241     if (RHS->equalsInt(1)) {
242       AddUsesToWorkList(I);          // Add all modified instrs to worklist
243       I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
244       return &I;
245     }
246   return 0;
247 }
248
249 static Constant *getMaxValue(const Type *Ty) {
250   assert(Ty == Type::BoolTy || Ty->isIntegral());
251   if (Ty == Type::BoolTy)
252     return ConstantBool::True;
253
254   if (Ty->isSigned())
255     return ConstantSInt::get(Ty, -1);
256   else if (Ty->isUnsigned()) {
257     // Calculate -1 casted to the right type...
258     unsigned TypeBits = Ty->getPrimitiveSize()*8;
259     uint64_t Val = (uint64_t)-1LL;       // All ones
260     Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
261     return ConstantUInt::get(Ty, Val);
262   }
263   return 0;
264 }
265
266
267 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
268   bool Changed = SimplifyBinOp(I);
269   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
270
271   // and X, X = X   and X, 0 == 0
272   if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) {
273     AddUsesToWorkList(I);            // Add all modified instrs to worklist
274     I.replaceAllUsesWith(Op1);
275     return &I;
276   }
277
278   // and X, -1 == X
279   if (Constant *RHS = dyn_cast<Constant>(Op1))
280     if (RHS == getMaxValue(I.getType())) {
281       AddUsesToWorkList(I);         // Add all modified instrs to worklist
282       I.replaceAllUsesWith(Op0);
283       return &I;
284     }
285
286   return Changed ? &I : 0;
287 }
288
289
290
291 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
292   bool Changed = SimplifyBinOp(I);
293   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
294
295   // or X, X = X   or X, 0 == X
296   if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) {
297     AddUsesToWorkList(I);         // Add all modified instrs to worklist
298     I.replaceAllUsesWith(Op0);
299     return &I;
300   }
301
302   // or X, -1 == -1
303   if (Constant *RHS = dyn_cast<Constant>(Op1))
304     if (RHS == getMaxValue(I.getType())) {
305       AddUsesToWorkList(I);         // Add all modified instrs to worklist
306       I.replaceAllUsesWith(Op1);
307       return &I;
308     }
309
310   return Changed ? &I : 0;
311 }
312
313
314
315 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
316   bool Changed = SimplifyBinOp(I);
317   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
318
319   // xor X, X = 0
320   if (Op0 == Op1) {
321     AddUsesToWorkList(I);         // Add all modified instrs to worklist
322     I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
323     return &I;
324   }
325
326   // xor X, 0 == X
327   if (Op1 == Constant::getNullValue(I.getType())) {
328     AddUsesToWorkList(I);         // Add all modified instrs to worklist
329     I.replaceAllUsesWith(Op0);
330     return &I;
331   }
332
333   return Changed ? &I : 0;
334 }
335
336 // isTrueWhenEqual - Return true if the specified setcondinst instruction is
337 // true when both operands are equal...
338 //
339 static bool isTrueWhenEqual(Instruction &I) {
340   return I.getOpcode() == Instruction::SetEQ ||
341          I.getOpcode() == Instruction::SetGE ||
342          I.getOpcode() == Instruction::SetLE;
343 }
344
345 Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
346   bool Changed = SimplifyBinOp(I);
347
348   // setcc X, X
349   if (I.getOperand(0) == I.getOperand(1)) {
350     AddUsesToWorkList(I);         // Add all modified instrs to worklist
351     I.replaceAllUsesWith(ConstantBool::get(isTrueWhenEqual(I)));
352     return &I;
353   }
354
355   // setcc <global*>, 0 - Global value addresses are never null!
356   if (isa<GlobalValue>(I.getOperand(0)) &&
357       isa<ConstantPointerNull>(I.getOperand(1))) {
358     AddUsesToWorkList(I);         // Add all modified instrs to worklist
359     I.replaceAllUsesWith(ConstantBool::get(!isTrueWhenEqual(I)));
360     return &I;
361   }
362
363   return Changed ? &I : 0;
364 }
365
366
367
368 Instruction *InstCombiner::visitShiftInst(Instruction &I) {
369   assert(I.getOperand(1)->getType() == Type::UByteTy);
370   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
371
372   // shl X, 0 == X and shr X, 0 == X
373   // shl 0, X == 0 and shr 0, X == 0
374   if (Op1 == Constant::getNullValue(Type::UByteTy) ||
375       Op0 == Constant::getNullValue(Op0->getType())) {
376     AddUsesToWorkList(I);         // Add all modified instrs to worklist
377     I.replaceAllUsesWith(Op0);
378     return &I;
379   }
380
381   // shl int X, 32 = 0 and shr sbyte Y, 9 = 0, ... just don't eliminate shr of
382   // a signed value.
383   //
384   if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
385     unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
386     if (CUI->getValue() >= TypeBits &&
387         !(Op0->getType()->isSigned() && I.getOpcode() == Instruction::Shr)) {
388       AddUsesToWorkList(I);         // Add all modified instrs to worklist
389       I.replaceAllUsesWith(Constant::getNullValue(Op0->getType()));
390       return &I;
391     }
392   }
393   return 0;
394 }
395
396
397 // isEliminableCastOfCast - Return true if it is valid to eliminate the CI
398 // instruction.
399 //
400 static inline bool isEliminableCastOfCast(const CastInst &CI,
401                                           const CastInst *CSrc) {
402   assert(CI.getOperand(0) == CSrc);
403   const Type *SrcTy = CSrc->getOperand(0)->getType();
404   const Type *MidTy = CSrc->getType();
405   const Type *DstTy = CI.getType();
406
407   // It is legal to eliminate the instruction if casting A->B->A if the sizes
408   // are identical and the bits don't get reinterpreted (for example 
409   // int->float->int)
410   if (SrcTy == DstTy && SrcTy->isLosslesslyConvertableTo(MidTy))
411     return true;
412
413   // Allow free casting and conversion of sizes as long as the sign doesn't
414   // change...
415   if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral() &&
416       SrcTy->isSigned() == MidTy->isSigned() &&
417       MidTy->isSigned() == DstTy->isSigned()) {
418     // Only accept cases where we are either monotonically increasing the type
419     // size, or monotonically decreasing it.
420     //
421     unsigned SrcSize = SrcTy->getPrimitiveSize();
422     unsigned MidSize = MidTy->getPrimitiveSize();
423     unsigned DstSize = DstTy->getPrimitiveSize();
424     if (SrcSize < MidSize && MidSize < DstSize)
425       return true;
426
427     if (SrcSize > MidSize && MidSize > DstSize)
428       return true;
429   }
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
440 //
441 Instruction *InstCombiner::visitCastInst(CastInst &CI) {
442   // If the user is casting a value to the same type, eliminate this cast
443   // instruction...
444   if (CI.getType() == CI.getOperand(0)->getType()) {
445     AddUsesToWorkList(CI);         // Add all modified instrs to worklist
446     CI.replaceAllUsesWith(CI.getOperand(0));
447     return &CI;
448   }
449
450   // If casting the result of another cast instruction, try to eliminate this
451   // one!
452   //
453   if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) {
454     if (isEliminableCastOfCast(CI, CSrc)) {
455       // This instruction now refers directly to the cast's src operand.  This
456       // has a good chance of making CSrc dead.
457       CI.setOperand(0, CSrc->getOperand(0));
458       return &CI;
459     }
460
461     // If this is an A->B->A cast, and we are dealing with integral types, try
462     // to convert this into a logical 'and' instruction.
463     //
464     if (CSrc->getOperand(0)->getType() == CI.getType() &&
465         CI.getType()->isIntegral() && CSrc->getType()->isIntegral() &&
466         CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
467         CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
468       assert(CSrc->getType() != Type::ULongTy &&
469              "Cannot have type bigger than ulong!");
470       unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1;
471       Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
472       return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
473                                     AndOp);
474     }
475   }
476
477   return 0;
478 }
479
480
481 // PHINode simplification
482 //
483 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
484   // If the PHI node only has one incoming value, eliminate the PHI node...
485   if (PN.getNumIncomingValues() == 1) {
486     AddUsesToWorkList(PN);         // Add all modified instrs to worklist
487     PN.replaceAllUsesWith(PN.getIncomingValue(0));
488     return &PN;
489   }
490
491   return 0;
492 }
493
494
495 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
496   // Is it 'getelementptr %P, uint 0'  or 'getelementptr %P'
497   // If so, eliminate the noop.
498   if ((GEP.getNumOperands() == 2 &&
499        GEP.getOperand(1) == Constant::getNullValue(Type::UIntTy)) ||
500       GEP.getNumOperands() == 1) {
501     AddUsesToWorkList(GEP);         // Add all modified instrs to worklist
502     GEP.replaceAllUsesWith(GEP.getOperand(0));
503     return &GEP;
504   }
505
506   // Combine Indices - If the source pointer to this getelementptr instruction
507   // is a getelementptr instruction, combine the indices of the two
508   // getelementptr instructions into a single instruction.
509   //
510   if (GetElementPtrInst *Src =
511       dyn_cast<GetElementPtrInst>(GEP.getPointerOperand())) {
512     std::vector<Value *> Indices;
513   
514     // Can we combine the two pointer arithmetics offsets?
515     if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
516         isa<Constant>(GEP.getOperand(1))) {
517       // Replace the index list on this GEP with the index on the getelementptr
518       Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
519       Indices[0] = *cast<Constant>(Src->getOperand(1)) +
520                    *cast<Constant>(GEP.getOperand(1));
521       assert(Indices[0] != 0 && "Constant folding of uint's failed!?");
522
523     } else if (*GEP.idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) { 
524       // Otherwise we can do the fold if the first index of the GEP is a zero
525       Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
526       Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
527     }
528
529     if (!Indices.empty())
530       return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
531   }
532
533   return 0;
534 }
535
536
537 bool InstCombiner::runOnFunction(Function &F) {
538   bool Changed = false;
539
540   WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
541
542   while (!WorkList.empty()) {
543     Instruction *I = WorkList.back();  // Get an instruction from the worklist
544     WorkList.pop_back();
545
546     // Now that we have an instruction, try combining it to simplify it...
547     if (Instruction *Result = visit(*I)) {
548       ++NumCombined;
549       // Should we replace the old instruction with a new one?
550       if (Result != I) {
551         // Instructions can end up on the worklist more than once.  Make sure
552         // we do not process an instruction that has been deleted.
553         WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
554                        WorkList.end());
555
556         ReplaceInstWithInst(I, Result);
557       } else {
558         BasicBlock::iterator II = I;
559
560         // If the instruction was modified, it's possible that it is now dead.
561         // if so, remove it.
562         if (dceInstruction(II)) {
563           // Instructions may end up in the worklist more than once.  Erase them
564           // all.
565           WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
566                          WorkList.end());
567           Result = 0;
568         }
569       }
570
571       if (Result) {
572         WorkList.push_back(Result);
573         AddUsesToWorkList(*Result);
574       }
575       Changed = true;
576     }
577   }
578
579   return Changed;
580 }
581
582 Pass *createInstructionCombiningPass() {
583   return new InstCombiner();
584 }