- Rename AnalysisUsage::preservesAll to getPreservesAll & preservesCFG to
[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 This pass is where algebraic
5 // simplification happens.
6 //
7 // This pass combines things like:
8 //    %Y = add int 1, %X
9 //    %Z = add int 1, %Y
10 // into:
11 //    %Z = add int 2, %X
12 //
13 // This is a simple worklist driven algorithm.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
19 #include "llvm/Transforms/Utils/Local.h"
20 #include "llvm/ConstantHandling.h"
21 #include "llvm/iMemory.h"
22 #include "llvm/iOther.h"
23 #include "llvm/iPHINode.h"
24 #include "llvm/iOperators.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/InstIterator.h"
27 #include "llvm/Support/InstVisitor.h"
28 #include "Support/Statistic.h"
29 #include <algorithm>
30
31 namespace {
32   Statistic<> NumCombined ("instcombine", "Number of insts combined");
33   Statistic<> NumConstProp("instcombine", "Number of constant folds");
34   Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated");
35
36   class InstCombiner : public FunctionPass,
37                        public InstVisitor<InstCombiner, Instruction*> {
38     // Worklist of all of the instructions that need to be simplified.
39     std::vector<Instruction*> WorkList;
40
41     void AddUsesToWorkList(Instruction &I) {
42       // The instruction was simplified, add all users of the instruction to
43       // the work lists because they might get more simplified now...
44       //
45       for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
46            UI != UE; ++UI)
47         WorkList.push_back(cast<Instruction>(*UI));
48     }
49
50     // removeFromWorkList - remove all instances of I from the worklist.
51     void removeFromWorkList(Instruction *I);
52   public:
53     virtual bool runOnFunction(Function &F);
54
55     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56       AU.setPreservesCFG();
57     }
58
59     // Visitation implementation - Implement instruction combining for different
60     // instruction types.  The semantics are as follows:
61     // Return Value:
62     //    null        - No change was made
63     //     I          - Change was made, I is still valid, I may be dead though
64     //   otherwise    - Change was made, replace I with returned instruction
65     //   
66     Instruction *visitAdd(BinaryOperator &I);
67     Instruction *visitSub(BinaryOperator &I);
68     Instruction *visitMul(BinaryOperator &I);
69     Instruction *visitDiv(BinaryOperator &I);
70     Instruction *visitRem(BinaryOperator &I);
71     Instruction *visitAnd(BinaryOperator &I);
72     Instruction *visitOr (BinaryOperator &I);
73     Instruction *visitXor(BinaryOperator &I);
74     Instruction *visitSetCondInst(BinaryOperator &I);
75     Instruction *visitShiftInst(Instruction &I);
76     Instruction *visitCastInst(CastInst &CI);
77     Instruction *visitPHINode(PHINode &PN);
78     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
79
80     // visitInstruction - Specify what to return for unhandled instructions...
81     Instruction *visitInstruction(Instruction &I) { return 0; }
82
83     // InsertNewInstBefore - insert an instruction New before instruction Old
84     // in the program.  Add the new instruction to the worklist.
85     //
86     void InsertNewInstBefore(Instruction *New, Instruction &Old) {
87       assert(New && New->getParent() == 0 &&
88              "New instruction already inserted into a basic block!");
89       BasicBlock *BB = Old.getParent();
90       BB->getInstList().insert(&Old, New);  // Insert inst
91       WorkList.push_back(New);              // Add to worklist
92     }
93
94     // ReplaceInstUsesWith - This method is to be used when an instruction is
95     // found to be dead, replacable with another preexisting expression.  Here
96     // we add all uses of I to the worklist, replace all uses of I with the new
97     // value, then return I, so that the inst combiner will know that I was
98     // modified.
99     //
100     Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
101       AddUsesToWorkList(I);         // Add all modified instrs to worklist
102       I.replaceAllUsesWith(V);
103       return &I;
104     }
105   };
106
107   RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
108 }
109
110
111 // Make sure that this instruction has a constant on the right hand side if it
112 // has any constant arguments.  If not, fix it an return true.
113 //
114 static bool SimplifyBinOp(BinaryOperator &I) {
115   if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1)))
116     return !I.swapOperands();
117   return false;
118 }
119
120 // dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
121 // instruction if the LHS is a constant zero (which is the 'negate' form).
122 //
123 static inline Value *dyn_castNegInst(Value *V) {
124   Instruction *I = dyn_cast<Instruction>(V);
125   if (!I || I->getOpcode() != Instruction::Sub) return 0;
126
127   if (I->getOperand(0) == Constant::getNullValue(I->getType()))
128     return I->getOperand(1);
129   return 0;
130 }
131
132 static inline Value *dyn_castNotInst(Value *V) {
133   Instruction *I = dyn_cast<Instruction>(V);
134   if (!I || I->getOpcode() != Instruction::Xor) return 0;
135
136   if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(I->getOperand(1)))
137     if (CI->isAllOnesValue())
138       return I->getOperand(0);
139   return 0;
140 }
141
142 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
143   bool Changed = SimplifyBinOp(I);
144   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
145
146   // Eliminate 'add int %X, 0'
147   if (RHS == Constant::getNullValue(I.getType()))
148     return ReplaceInstUsesWith(I, LHS);
149
150   // -A + B  -->  B - A
151   if (Value *V = dyn_castNegInst(LHS))
152     return BinaryOperator::create(Instruction::Sub, RHS, V);
153
154   // A + -B  -->  A - B
155   if (Value *V = dyn_castNegInst(RHS))
156     return BinaryOperator::create(Instruction::Sub, LHS, V);
157
158   // Simplify add instructions with a constant RHS...
159   if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
160     if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
161       if (ILHS->getOpcode() == Instruction::Add &&
162           isa<Constant>(ILHS->getOperand(1))) {
163         // Fold:
164         //    %Y = add int %X, 1
165         //    %Z = add int %Y, 1
166         // into:
167         //    %Z = add int %X, 2
168         //
169         if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
170           I.setOperand(0, ILHS->getOperand(0));
171           I.setOperand(1, Val);
172           return &I;
173         }
174       }
175     }
176   }
177
178   return Changed ? &I : 0;
179 }
180
181 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
182   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
183
184   if (Op0 == Op1)         // sub X, X  -> 0
185     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
186
187   // If this is a subtract instruction with a constant RHS, convert it to an add
188   // instruction of a negative constant
189   //
190   if (Constant *Op2 = dyn_cast<Constant>(Op1))
191     if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS
192       return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName());
193
194   // If this is a 'B = x-(-A)', change to B = x+A...
195   if (Value *V = dyn_castNegInst(Op1))
196     return BinaryOperator::create(Instruction::Add, Op0, V);
197
198   // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
199   // not used by anyone else...
200   //
201   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
202     if (Op1I->use_size() == 1 && Op1I->getOpcode() == Instruction::Sub) {
203       // Swap the two operands of the subexpr...
204       Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
205       Op1I->setOperand(0, IIOp1);
206       Op1I->setOperand(1, IIOp0);
207
208       // Create the new top level add instruction...
209       return BinaryOperator::create(Instruction::Add, Op0, Op1);
210     }
211   return 0;
212 }
213
214 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
215   bool Changed = SimplifyBinOp(I);
216   Value *Op1 = I.getOperand(0);
217
218   // Simplify mul instructions with a constant RHS...
219   if (Constant *Op2 = dyn_cast<Constant>(I.getOperand(1))) {
220     if (I.getType()->isInteger() && cast<ConstantInt>(Op2)->equalsInt(1))
221       return ReplaceInstUsesWith(I, Op1);  // Eliminate 'mul int %X, 1'
222
223     if (I.getType()->isInteger() && cast<ConstantInt>(Op2)->equalsInt(2))
224       // Convert 'mul int %X, 2' to 'add int %X, %X'
225       return BinaryOperator::create(Instruction::Add, Op1, Op1, I.getName());
226
227     if (Op2->isNullValue())
228       return ReplaceInstUsesWith(I, Op2);  // Eliminate 'mul int %X, 0'
229   }
230
231   return Changed ? &I : 0;
232 }
233
234
235 Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
236   // div X, 1 == X
237   if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
238     if (RHS->equalsInt(1))
239       return ReplaceInstUsesWith(I, I.getOperand(0));
240   return 0;
241 }
242
243
244 Instruction *InstCombiner::visitRem(BinaryOperator &I) {
245   // rem X, 1 == 0
246   if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
247     if (RHS->equalsInt(1))
248       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
249
250   return 0;
251 }
252
253 // isMaxValueMinusOne - return true if this is Max-1
254 static bool isMaxValueMinusOne(const ConstantInt *C) {
255   if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) {
256     // Calculate -1 casted to the right type...
257     unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
258     uint64_t Val = ~0ULL;                // All ones
259     Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
260     return CU->getValue() == Val-1;
261   }
262
263   const ConstantSInt *CS = cast<ConstantSInt>(C);
264   
265   // Calculate 0111111111..11111
266   unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
267   int64_t Val = INT64_MAX;             // All ones
268   Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
269   return CS->getValue() == Val-1;
270 }
271
272 // isMinValuePlusOne - return true if this is Min+1
273 static bool isMinValuePlusOne(const ConstantInt *C) {
274   if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
275     return CU->getValue() == 1;
276
277   const ConstantSInt *CS = cast<ConstantSInt>(C);
278   
279   // Calculate 1111111111000000000000 
280   unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
281   int64_t Val = -1;                    // All ones
282   Val <<= TypeBits-1;                  // Shift over to the right spot
283   return CS->getValue() == Val+1;
284 }
285
286
287 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
288   bool Changed = SimplifyBinOp(I);
289   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
290
291   // and X, X = X   and X, 0 == 0
292   if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
293     return ReplaceInstUsesWith(I, Op1);
294
295   // and X, -1 == X
296   if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
297     if (RHS->isAllOnesValue())
298       return ReplaceInstUsesWith(I, Op0);
299
300   // and (not A), (not B) == not (or A, B)
301   if (Op0->use_size() == 1 && Op1->use_size() == 1)
302     if (Value *A = dyn_castNotInst(Op0))
303       if (Value *B = dyn_castNotInst(Op1)) {
304         Instruction *Or = BinaryOperator::create(Instruction::Or, A, B,
305                                                  I.getName()+".demorgan");
306         InsertNewInstBefore(Or, I);
307         return BinaryOperator::createNot(Or, I.getName());
308       }
309
310   return Changed ? &I : 0;
311 }
312
313
314
315 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
316   bool Changed = SimplifyBinOp(I);
317   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
318
319   // or X, X = X   or X, 0 == X
320   if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
321     return ReplaceInstUsesWith(I, Op0);
322
323   // or X, -1 == -1
324   if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
325     if (RHS->isAllOnesValue())
326       return ReplaceInstUsesWith(I, Op1);
327
328   return Changed ? &I : 0;
329 }
330
331
332
333 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
334   bool Changed = SimplifyBinOp(I);
335   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
336
337   // xor X, X = 0
338   if (Op0 == Op1)
339     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
340
341   if (ConstantIntegral *Op1C = dyn_cast<ConstantIntegral>(Op1)) {
342     // xor X, 0 == X
343     if (Op1C->isNullValue())
344       return ReplaceInstUsesWith(I, Op0);
345
346     // Is this a "NOT" instruction?
347     if (Op1C->isAllOnesValue()) {
348       // xor (xor X, -1), -1 = not (not X) = X
349       if (Value *X = dyn_castNotInst(Op0))
350         return ReplaceInstUsesWith(I, X);
351
352       // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
353       if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0))
354         if (SCI->use_size() == 1)
355           return new SetCondInst(SCI->getInverseCondition(),
356                                  SCI->getOperand(0), SCI->getOperand(1));
357     }
358   }
359
360   return Changed ? &I : 0;
361 }
362
363 // AddOne, SubOne - Add or subtract a constant one from an integer constant...
364 static Constant *AddOne(ConstantInt *C) {
365   Constant *Result = *C + *ConstantInt::get(C->getType(), 1);
366   assert(Result && "Constant folding integer addition failed!");
367   return Result;
368 }
369 static Constant *SubOne(ConstantInt *C) {
370   Constant *Result = *C - *ConstantInt::get(C->getType(), 1);
371   assert(Result && "Constant folding integer addition failed!");
372   return Result;
373 }
374
375 // isTrueWhenEqual - Return true if the specified setcondinst instruction is
376 // true when both operands are equal...
377 //
378 static bool isTrueWhenEqual(Instruction &I) {
379   return I.getOpcode() == Instruction::SetEQ ||
380          I.getOpcode() == Instruction::SetGE ||
381          I.getOpcode() == Instruction::SetLE;
382 }
383
384 Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
385   bool Changed = SimplifyBinOp(I);
386   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
387   const Type *Ty = Op0->getType();
388
389   // setcc X, X
390   if (Op0 == Op1)
391     return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
392
393   // setcc <global*>, 0 - Global value addresses are never null!
394   if (isa<GlobalValue>(Op0) && isa<ConstantPointerNull>(Op1))
395     return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
396
397   // setcc's with boolean values can always be turned into bitwise operations
398   if (Ty == Type::BoolTy) {
399     // If this is <, >, or !=, we can change this into a simple xor instruction
400     if (!isTrueWhenEqual(I))
401       return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName());
402
403     // Otherwise we need to make a temporary intermediate instruction and insert
404     // it into the instruction stream.  This is what we are after:
405     //
406     //  seteq bool %A, %B -> ~(A^B)
407     //  setle bool %A, %B -> ~A | B
408     //  setge bool %A, %B -> A | ~B
409     //
410     if (I.getOpcode() == Instruction::SetEQ) {  // seteq case
411       Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1,
412                                                 I.getName()+"tmp");
413       InsertNewInstBefore(Xor, I);
414       return BinaryOperator::createNot(Xor, I.getName());
415     }
416
417     // Handle the setXe cases...
418     assert(I.getOpcode() == Instruction::SetGE ||
419            I.getOpcode() == Instruction::SetLE);
420
421     if (I.getOpcode() == Instruction::SetGE)
422       std::swap(Op0, Op1);                   // Change setge -> setle
423
424     // Now we just have the SetLE case.
425     Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
426     InsertNewInstBefore(Not, I);
427     return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName());
428   }
429
430   // Check to see if we are doing one of many comparisons against constant
431   // integers at the end of their ranges...
432   //
433   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
434     // Check to see if we are comparing against the minimum or maximum value...
435     if (CI->isMinValue()) {
436       if (I.getOpcode() == Instruction::SetLT)       // A < MIN -> FALSE
437         return ReplaceInstUsesWith(I, ConstantBool::False);
438       if (I.getOpcode() == Instruction::SetGE)       // A >= MIN -> TRUE
439         return ReplaceInstUsesWith(I, ConstantBool::True);
440       if (I.getOpcode() == Instruction::SetLE)       // A <= MIN -> A == MIN
441         return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
442       if (I.getOpcode() == Instruction::SetGT)       // A > MIN -> A != MIN
443         return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
444
445     } else if (CI->isMaxValue()) {
446       if (I.getOpcode() == Instruction::SetGT)       // A > MAX -> FALSE
447         return ReplaceInstUsesWith(I, ConstantBool::False);
448       if (I.getOpcode() == Instruction::SetLE)       // A <= MAX -> TRUE
449         return ReplaceInstUsesWith(I, ConstantBool::True);
450       if (I.getOpcode() == Instruction::SetGE)       // A >= MAX -> A == MAX
451         return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
452       if (I.getOpcode() == Instruction::SetLT)       // A < MAX -> A != MAX
453         return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
454
455       // Comparing against a value really close to min or max?
456     } else if (isMinValuePlusOne(CI)) {
457       if (I.getOpcode() == Instruction::SetLT)       // A < MIN+1 -> A == MIN
458         return BinaryOperator::create(Instruction::SetEQ, Op0,
459                                       SubOne(CI), I.getName());
460       if (I.getOpcode() == Instruction::SetGE)       // A >= MIN-1 -> A != MIN
461         return BinaryOperator::create(Instruction::SetNE, Op0,
462                                       SubOne(CI), I.getName());
463
464     } else if (isMaxValueMinusOne(CI)) {
465       if (I.getOpcode() == Instruction::SetGT)       // A > MAX-1 -> A == MAX
466         return BinaryOperator::create(Instruction::SetEQ, Op0,
467                                       AddOne(CI), I.getName());
468       if (I.getOpcode() == Instruction::SetLE)       // A <= MAX-1 -> A != MAX
469         return BinaryOperator::create(Instruction::SetNE, Op0,
470                                       AddOne(CI), I.getName());
471     }
472   }
473
474   return Changed ? &I : 0;
475 }
476
477
478
479 Instruction *InstCombiner::visitShiftInst(Instruction &I) {
480   assert(I.getOperand(1)->getType() == Type::UByteTy);
481   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
482
483   // shl X, 0 == X and shr X, 0 == X
484   // shl 0, X == 0 and shr 0, X == 0
485   if (Op1 == Constant::getNullValue(Type::UByteTy) ||
486       Op0 == Constant::getNullValue(Op0->getType()))
487     return ReplaceInstUsesWith(I, Op0);
488
489   // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr of
490   // a signed value.
491   //
492   if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
493     if (I.getOpcode() == Instruction::Shr) {
494       unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
495       if (CUI->getValue() >= TypeBits && !(Op0->getType()->isSigned()))
496         return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
497     }
498
499     // Check to see if we are shifting left by 1.  If so, turn it into an add
500     // instruction.
501     if (I.getOpcode() == Instruction::Shl && CUI->equalsInt(1))
502       // Convert 'shl int %X, 2' to 'add int %X, %X'
503       return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
504
505   }
506
507   // shr int -1, X = -1   (for any arithmetic shift rights of ~0)
508   if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
509     if (I.getOpcode() == Instruction::Shr && CSI->isAllOnesValue())
510       return ReplaceInstUsesWith(I, CSI);
511   
512   return 0;
513 }
514
515
516 // isEliminableCastOfCast - Return true if it is valid to eliminate the CI
517 // instruction.
518 //
519 static inline bool isEliminableCastOfCast(const CastInst &CI,
520                                           const CastInst *CSrc) {
521   assert(CI.getOperand(0) == CSrc);
522   const Type *SrcTy = CSrc->getOperand(0)->getType();
523   const Type *MidTy = CSrc->getType();
524   const Type *DstTy = CI.getType();
525
526   // It is legal to eliminate the instruction if casting A->B->A if the sizes
527   // are identical and the bits don't get reinterpreted (for example 
528   // int->float->int would not be allowed)
529   if (SrcTy == DstTy && SrcTy->isLosslesslyConvertableTo(MidTy))
530     return true;
531
532   // Allow free casting and conversion of sizes as long as the sign doesn't
533   // change...
534   if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
535     unsigned SrcSize = SrcTy->getPrimitiveSize();
536     unsigned MidSize = MidTy->getPrimitiveSize();
537     unsigned DstSize = DstTy->getPrimitiveSize();
538
539     // Cases where we are monotonically decreasing the size of the type are
540     // always ok, regardless of what sign changes are going on.
541     //
542     if (SrcSize >= MidSize && MidSize >= DstSize)
543       return true;
544
545     // Cases where the source and destination type are the same, but the middle
546     // type is bigger are noops.
547     //
548     if (SrcSize == DstSize && MidSize > SrcSize)
549       return true;
550
551     // If we are monotonically growing, things are more complex.
552     //
553     if (SrcSize <= MidSize && MidSize <= DstSize) {
554       // We have eight combinations of signedness to worry about. Here's the
555       // table:
556       static const int SignTable[8] = {
557         // CODE, SrcSigned, MidSigned, DstSigned, Comment
558         1,     //   U          U          U       Always ok
559         1,     //   U          U          S       Always ok
560         3,     //   U          S          U       Ok iff SrcSize != MidSize
561         3,     //   U          S          S       Ok iff SrcSize != MidSize
562         0,     //   S          U          U       Never ok
563         2,     //   S          U          S       Ok iff MidSize == DstSize
564         1,     //   S          S          U       Always ok
565         1,     //   S          S          S       Always ok
566       };
567
568       // Choose an action based on the current entry of the signtable that this
569       // cast of cast refers to...
570       unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned();
571       switch (SignTable[Row]) {
572       case 0: return false;              // Never ok
573       case 1: return true;               // Always ok
574       case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize
575       case 3:                            // Ok iff SrcSize != MidSize
576         return SrcSize != MidSize || SrcTy == Type::BoolTy;
577       default: assert(0 && "Bad entry in sign table!");
578       }
579     }
580   }
581
582   // Otherwise, we cannot succeed.  Specifically we do not want to allow things
583   // like:  short -> ushort -> uint, because this can create wrong results if
584   // the input short is negative!
585   //
586   return false;
587 }
588
589
590 // CastInst simplification
591 //
592 Instruction *InstCombiner::visitCastInst(CastInst &CI) {
593   // If the user is casting a value to the same type, eliminate this cast
594   // instruction...
595   if (CI.getType() == CI.getOperand(0)->getType())
596     return ReplaceInstUsesWith(CI, CI.getOperand(0));
597
598   // If casting the result of another cast instruction, try to eliminate this
599   // one!
600   //
601   if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) {
602     if (isEliminableCastOfCast(CI, CSrc)) {
603       // This instruction now refers directly to the cast's src operand.  This
604       // has a good chance of making CSrc dead.
605       CI.setOperand(0, CSrc->getOperand(0));
606       return &CI;
607     }
608
609     // If this is an A->B->A cast, and we are dealing with integral types, try
610     // to convert this into a logical 'and' instruction.
611     //
612     if (CSrc->getOperand(0)->getType() == CI.getType() &&
613         CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
614         CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
615         CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
616       assert(CSrc->getType() != Type::ULongTy &&
617              "Cannot have type bigger than ulong!");
618       unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1;
619       Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
620       return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
621                                     AndOp);
622     }
623   }
624
625   return 0;
626 }
627
628
629 // PHINode simplification
630 //
631 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
632   // If the PHI node only has one incoming value, eliminate the PHI node...
633   if (PN.getNumIncomingValues() == 1)
634     return ReplaceInstUsesWith(PN, PN.getIncomingValue(0));
635   
636   // Otherwise if all of the incoming values are the same for the PHI, replace
637   // the PHI node with the incoming value.
638   //
639   Value *InVal = 0;
640   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
641     if (PN.getIncomingValue(i) != &PN)  // Not the PHI node itself...
642       if (InVal && PN.getIncomingValue(i) != InVal)
643         return 0;  // Not the same, bail out.
644       else
645         InVal = PN.getIncomingValue(i);
646
647   // The only case that could cause InVal to be null is if we have a PHI node
648   // that only has entries for itself.  In this case, there is no entry into the
649   // loop, so kill the PHI.
650   //
651   if (InVal == 0) InVal = Constant::getNullValue(PN.getType());
652
653   // All of the incoming values are the same, replace the PHI node now.
654   return ReplaceInstUsesWith(PN, InVal);
655 }
656
657
658 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
659   // Is it 'getelementptr %P, uint 0'  or 'getelementptr %P'
660   // If so, eliminate the noop.
661   if ((GEP.getNumOperands() == 2 &&
662        GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) ||
663       GEP.getNumOperands() == 1)
664     return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
665
666   // Combine Indices - If the source pointer to this getelementptr instruction
667   // is a getelementptr instruction, combine the indices of the two
668   // getelementptr instructions into a single instruction.
669   //
670   if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) {
671     std::vector<Value *> Indices;
672   
673     // Can we combine the two pointer arithmetics offsets?
674     if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
675         isa<Constant>(GEP.getOperand(1))) {
676       // Replace the index list on this GEP with the index on the getelementptr
677       Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
678       Indices[0] = *cast<Constant>(Src->getOperand(1)) +
679                    *cast<Constant>(GEP.getOperand(1));
680       assert(Indices[0] != 0 && "Constant folding of uint's failed!?");
681
682     } else if (*GEP.idx_begin() == ConstantUInt::getNullValue(Type::LongTy) &&
683                Src->getNumOperands() != 1) { 
684       // Otherwise we can do the fold if the first index of the GEP is a zero
685       Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
686       Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
687     }
688
689     if (!Indices.empty())
690       return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
691
692   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) {
693     // GEP of global variable.  If all of the indices for this GEP are
694     // constants, we can promote this to a constexpr instead of an instruction.
695
696     // Scan for nonconstants...
697     std::vector<Constant*> Indices;
698     User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
699     for (; I != E && isa<Constant>(*I); ++I)
700       Indices.push_back(cast<Constant>(*I));
701
702     if (I == E) {  // If they are all constants...
703       ConstantExpr *CE =
704         ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices);
705
706       // Replace all uses of the GEP with the new constexpr...
707       return ReplaceInstUsesWith(GEP, CE);
708     }
709   }
710
711   return 0;
712 }
713
714
715 void InstCombiner::removeFromWorkList(Instruction *I) {
716   WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
717                  WorkList.end());
718 }
719
720 bool InstCombiner::runOnFunction(Function &F) {
721   bool Changed = false;
722
723   WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
724
725   while (!WorkList.empty()) {
726     Instruction *I = WorkList.back();  // Get an instruction from the worklist
727     WorkList.pop_back();
728
729     // Check to see if we can DCE or ConstantPropogate the instruction...
730     // Check to see if we can DIE the instruction...
731     if (isInstructionTriviallyDead(I)) {
732       // Add operands to the worklist...
733       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
734         if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
735           WorkList.push_back(Op);
736
737       ++NumDeadInst;
738       BasicBlock::iterator BBI = I;
739       if (dceInstruction(BBI)) {
740         removeFromWorkList(I);
741         continue;
742       }
743     } 
744
745     // Instruction isn't dead, see if we can constant propogate it...
746     if (Constant *C = ConstantFoldInstruction(I)) {
747       // Add operands to the worklist...
748       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
749         if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
750           WorkList.push_back(Op);
751       I->replaceAllUsesWith(C);
752       ++NumConstProp;
753       BasicBlock::iterator BBI = I;
754       if (dceInstruction(BBI)) {
755         removeFromWorkList(I);
756         continue;
757       }
758     }
759     
760     // Now that we have an instruction, try combining it to simplify it...
761     if (Instruction *Result = visit(*I)) {
762       ++NumCombined;
763       // Should we replace the old instruction with a new one?
764       if (Result != I) {
765         // Instructions can end up on the worklist more than once.  Make sure
766         // we do not process an instruction that has been deleted.
767         removeFromWorkList(I);
768         ReplaceInstWithInst(I, Result);
769       } else {
770         BasicBlock::iterator II = I;
771
772         // If the instruction was modified, it's possible that it is now dead.
773         // if so, remove it.
774         if (dceInstruction(II)) {
775           // Instructions may end up in the worklist more than once.  Erase them
776           // all.
777           removeFromWorkList(I);
778           Result = 0;
779         }
780       }
781
782       if (Result) {
783         WorkList.push_back(Result);
784         AddUsesToWorkList(*Result);
785       }
786       Changed = true;
787     }
788   }
789
790   return Changed;
791 }
792
793 Pass *createInstructionCombiningPass() {
794   return new InstCombiner();
795 }