Add new transformation: // (~A | ~B) == (~(A & B))
[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/DerivedTypes.h"
27 #include "llvm/Support/InstIterator.h"
28 #include "llvm/Support/InstVisitor.h"
29 #include "Support/Statistic.h"
30 #include <algorithm>
31
32 namespace {
33   Statistic<> NumCombined ("instcombine", "Number of insts combined");
34   Statistic<> NumConstProp("instcombine", "Number of constant folds");
35   Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated");
36
37   class InstCombiner : public FunctionPass,
38                        public InstVisitor<InstCombiner, Instruction*> {
39     // Worklist of all of the instructions that need to be simplified.
40     std::vector<Instruction*> WorkList;
41
42     void AddUsesToWorkList(Instruction &I) {
43       // The instruction was simplified, add all users of the instruction to
44       // the work lists because they might get more simplified now...
45       //
46       for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
47            UI != UE; ++UI)
48         WorkList.push_back(cast<Instruction>(*UI));
49     }
50
51     // removeFromWorkList - remove all instances of I from the worklist.
52     void removeFromWorkList(Instruction *I);
53   public:
54     virtual bool runOnFunction(Function &F);
55
56     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57       AU.setPreservesCFG();
58     }
59
60     // Visitation implementation - Implement instruction combining for different
61     // instruction types.  The semantics are as follows:
62     // Return Value:
63     //    null        - No change was made
64     //     I          - Change was made, I is still valid, I may be dead though
65     //   otherwise    - Change was made, replace I with returned instruction
66     //   
67     Instruction *visitAdd(BinaryOperator &I);
68     Instruction *visitSub(BinaryOperator &I);
69     Instruction *visitMul(BinaryOperator &I);
70     Instruction *visitDiv(BinaryOperator &I);
71     Instruction *visitRem(BinaryOperator &I);
72     Instruction *visitAnd(BinaryOperator &I);
73     Instruction *visitOr (BinaryOperator &I);
74     Instruction *visitXor(BinaryOperator &I);
75     Instruction *visitSetCondInst(BinaryOperator &I);
76     Instruction *visitShiftInst(ShiftInst &I);
77     Instruction *visitCastInst(CastInst &CI);
78     Instruction *visitPHINode(PHINode &PN);
79     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
80     Instruction *visitAllocationInst(AllocationInst &AI);
81
82     // visitInstruction - Specify what to return for unhandled instructions...
83     Instruction *visitInstruction(Instruction &I) { return 0; }
84
85     // InsertNewInstBefore - insert an instruction New before instruction Old
86     // in the program.  Add the new instruction to the worklist.
87     //
88     void InsertNewInstBefore(Instruction *New, Instruction &Old) {
89       assert(New && New->getParent() == 0 &&
90              "New instruction already inserted into a basic block!");
91       BasicBlock *BB = Old.getParent();
92       BB->getInstList().insert(&Old, New);  // Insert inst
93       WorkList.push_back(New);              // Add to worklist
94     }
95
96     // ReplaceInstUsesWith - This method is to be used when an instruction is
97     // found to be dead, replacable with another preexisting expression.  Here
98     // we add all uses of I to the worklist, replace all uses of I with the new
99     // value, then return I, so that the inst combiner will know that I was
100     // modified.
101     //
102     Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
103       AddUsesToWorkList(I);         // Add all modified instrs to worklist
104       I.replaceAllUsesWith(V);
105       return &I;
106     }
107   };
108
109   RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
110 }
111
112 // getComplexity:  Assign a complexity or rank value to LLVM Values...
113 //   0 -> Constant, 1 -> Other, 2 -> Argument, 2 -> Unary, 3 -> OtherInst
114 static unsigned getComplexity(Value *V) {
115   if (isa<Instruction>(V)) {
116     if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
117       return 2;
118     return 3;
119   }
120   if (isa<Argument>(V)) return 2;
121   return isa<Constant>(V) ? 0 : 1;
122 }
123
124 // SimplifyCommutative - This performs a few simplifications for commutative
125 // operators:
126 //
127 //  1. Order operands such that they are listed from right (least complex) to
128 //     left (most complex).  This puts constants before unary operators before
129 //     binary operators.
130 //
131 //  2. Handle the case of (op (op V, C1), C2), changing it to:
132 //     (op V, (op C1, C2))
133 //
134 static bool SimplifyCommutative(BinaryOperator &I) {
135   bool Changed = false;
136   if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
137     Changed = !I.swapOperands();
138   
139   if (!I.isAssociative()) return Changed;
140   Instruction::BinaryOps Opcode = I.getOpcode();
141   if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) {
142     if (Op->getOpcode() == Opcode && isa<Constant>(I.getOperand(1)) &&
143         isa<Constant>(Op->getOperand(1))) {
144       Instruction *New = BinaryOperator::create(I.getOpcode(), I.getOperand(1),
145                                                 Op->getOperand(1));
146       Constant *Folded = ConstantFoldInstruction(New);
147       delete New;
148       assert(Folded && "Couldn't constant fold commutative operand?");
149       I.setOperand(0, Op->getOperand(0));
150       I.setOperand(1, Folded);
151       return true;
152     }
153   }
154   return Changed;
155 }
156
157 // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
158 // if the LHS is a constant zero (which is the 'negate' form).
159 //
160 static inline Value *dyn_castNegVal(Value *V) {
161   if (BinaryOperator::isNeg(V))
162     return BinaryOperator::getNegArgument(cast<BinaryOperator>(V));
163
164   // Constants can be considered to be negated values...
165   if (Constant *C = dyn_cast<Constant>(V)) {
166     Constant *NC = *Constant::getNullValue(V->getType()) - *C;
167     assert(NC && "Couldn't constant fold a subtract!");
168     return NC;
169   }
170   return 0;
171 }
172
173 static inline Value *dyn_castNotVal(Value *V) {
174   if (BinaryOperator::isNot(V))
175     return BinaryOperator::getNotArgument(cast<BinaryOperator>(V));
176
177   // Constants can be considered to be not'ed values...
178   if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) {
179     Constant *NC = *ConstantIntegral::getAllOnesValue(C->getType()) ^ *C;
180     assert(NC && "Couldn't constant fold an exclusive or!");
181     return NC;
182   }
183   return 0;
184 }
185
186 static bool isOnlyUse(Value *V) {
187   return V->use_size() == 1 || isa<Constant>(V);
188 }
189
190
191 // Log2 - Calculate the log base 2 for the specified value if it is exactly a
192 // power of 2.
193 static unsigned Log2(uint64_t Val) {
194   assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!");
195   unsigned Count = 0;
196   while (Val != 1) {
197     if (Val & 1) return 0;    // Multiple bits set?
198     Val >>= 1;
199     ++Count;
200   }
201   return Count;
202 }
203
204 static inline Value *dyn_castFoldableMul(Value *V) {
205   if (V->use_size() == 1 && V->getType()->isInteger())
206     if (Instruction *I = dyn_cast<Instruction>(V))
207       if (I->getOpcode() == Instruction::Mul)
208         if (isa<Constant>(I->getOperand(1)))
209           return I->getOperand(0);
210   return 0;
211 }
212
213
214 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
215   bool Changed = SimplifyCommutative(I);
216   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
217
218   // Eliminate 'add int %X, 0'
219   if (RHS == Constant::getNullValue(I.getType()))
220     return ReplaceInstUsesWith(I, LHS);
221
222   // -A + B  -->  B - A
223   if (Value *V = dyn_castNegVal(LHS))
224     return BinaryOperator::create(Instruction::Sub, RHS, V);
225
226   // A + -B  -->  A - B
227   if (!isa<Constant>(RHS))
228     if (Value *V = dyn_castNegVal(RHS))
229       return BinaryOperator::create(Instruction::Sub, LHS, V);
230
231   // X*C + X --> X * (C+1)
232   if (dyn_castFoldableMul(LHS) == RHS) {
233     Constant *CP1 = *cast<Constant>(cast<Instruction>(LHS)->getOperand(1)) +
234                     *ConstantInt::get(I.getType(), 1);
235     assert(CP1 && "Couldn't constant fold C + 1?");
236     return BinaryOperator::create(Instruction::Mul, RHS, CP1);
237   }
238
239   // X + X*C --> X * (C+1)
240   if (dyn_castFoldableMul(RHS) == LHS) {
241     Constant *CP1 = *cast<Constant>(cast<Instruction>(RHS)->getOperand(1)) +
242                     *ConstantInt::get(I.getType(), 1);
243     assert(CP1 && "Couldn't constant fold C + 1?");
244     return BinaryOperator::create(Instruction::Mul, LHS, CP1);
245   }
246
247   return Changed ? &I : 0;
248 }
249
250 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
251   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
252
253   if (Op0 == Op1)         // sub X, X  -> 0
254     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
255
256   // If this is a 'B = x-(-A)', change to B = x+A...
257   if (Value *V = dyn_castNegVal(Op1))
258     return BinaryOperator::create(Instruction::Add, Op0, V);
259
260   // Replace (-1 - A) with (~A)...
261   if (ConstantInt *C = dyn_cast<ConstantInt>(Op0))
262     if (C->isAllOnesValue())
263       return BinaryOperator::createNot(Op1);
264
265   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
266     if (Op1I->use_size() == 1) {
267       // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
268       // is not used by anyone else...
269       //
270       if (Op1I->getOpcode() == Instruction::Sub) {
271         // Swap the two operands of the subexpr...
272         Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
273         Op1I->setOperand(0, IIOp1);
274         Op1I->setOperand(1, IIOp0);
275         
276         // Create the new top level add instruction...
277         return BinaryOperator::create(Instruction::Add, Op0, Op1);
278       }
279
280       // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
281       //
282       if (Op1I->getOpcode() == Instruction::And &&
283           (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
284         Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
285
286         Instruction *NewNot = BinaryOperator::createNot(OtherOp, "B.not", &I);
287         return BinaryOperator::create(Instruction::And, Op0, NewNot);
288       }
289
290       // X - X*C --> X * (1-C)
291       if (dyn_castFoldableMul(Op1I) == Op0) {
292         Constant *CP1 = *ConstantInt::get(I.getType(), 1) -
293                         *cast<Constant>(cast<Instruction>(Op1)->getOperand(1));
294         assert(CP1 && "Couldn't constant fold 1-C?");
295         return BinaryOperator::create(Instruction::Mul, Op0, CP1);
296       }
297     }
298
299   // X*C - X --> X * (C-1)
300   if (dyn_castFoldableMul(Op0) == Op1) {
301     Constant *CP1 = *cast<Constant>(cast<Instruction>(Op0)->getOperand(1)) -
302                     *ConstantInt::get(I.getType(), 1);
303     assert(CP1 && "Couldn't constant fold C - 1?");
304     return BinaryOperator::create(Instruction::Mul, Op1, CP1);
305   }
306
307   return 0;
308 }
309
310 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
311   bool Changed = SimplifyCommutative(I);
312   Value *Op0 = I.getOperand(0);
313
314   // Simplify mul instructions with a constant RHS...
315   if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
316     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
317       const Type *Ty = CI->getType();
318       uint64_t Val = Ty->isSigned() ?
319                           (uint64_t)cast<ConstantSInt>(CI)->getValue() : 
320                                     cast<ConstantUInt>(CI)->getValue();
321       switch (Val) {
322       case 0:
323         return ReplaceInstUsesWith(I, Op1);  // Eliminate 'mul double %X, 0'
324       case 1:
325         return ReplaceInstUsesWith(I, Op0);  // Eliminate 'mul int %X, 1'
326       case 2:                     // Convert 'mul int %X, 2' to 'add int %X, %X'
327         return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
328       }
329
330       if (uint64_t C = Log2(Val))            // Replace X*(2^C) with X << C
331         return new ShiftInst(Instruction::Shl, Op0,
332                              ConstantUInt::get(Type::UByteTy, C));
333     } else {
334       ConstantFP *Op1F = cast<ConstantFP>(Op1);
335       if (Op1F->isNullValue())
336         return ReplaceInstUsesWith(I, Op1);
337
338       // "In IEEE floating point, x*1 is not equivalent to x for nans.  However,
339       // ANSI says we can drop signals, so we can do this anyway." (from GCC)
340       if (Op1F->getValue() == 1.0)
341         return ReplaceInstUsesWith(I, Op0);  // Eliminate 'mul double %X, 1.0'
342     }
343   }
344
345   return Changed ? &I : 0;
346 }
347
348 Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
349   // div X, 1 == X
350   if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
351     if (RHS->equalsInt(1))
352       return ReplaceInstUsesWith(I, I.getOperand(0));
353
354     // Check to see if this is an unsigned division with an exact power of 2,
355     // if so, convert to a right shift.
356     if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
357       if (uint64_t Val = C->getValue())    // Don't break X / 0
358         if (uint64_t C = Log2(Val))
359           return new ShiftInst(Instruction::Shr, I.getOperand(0),
360                                ConstantUInt::get(Type::UByteTy, C));
361   }
362
363   // 0 / X == 0, we don't need to preserve faults!
364   if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
365     if (LHS->equalsInt(0))
366       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
367
368   return 0;
369 }
370
371
372 Instruction *InstCombiner::visitRem(BinaryOperator &I) {
373   if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
374     if (RHS->equalsInt(1))  // X % 1 == 0
375       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
376
377     // Check to see if this is an unsigned remainder with an exact power of 2,
378     // if so, convert to a bitwise and.
379     if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
380       if (uint64_t Val = C->getValue())    // Don't break X % 0 (divide by zero)
381         if (Log2(Val))
382           return BinaryOperator::create(Instruction::And, I.getOperand(0),
383                                         ConstantUInt::get(I.getType(), Val-1));
384   }
385
386   // 0 % X == 0, we don't need to preserve faults!
387   if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
388     if (LHS->equalsInt(0))
389       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
390
391   return 0;
392 }
393
394 // isMaxValueMinusOne - return true if this is Max-1
395 static bool isMaxValueMinusOne(const ConstantInt *C) {
396   if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) {
397     // Calculate -1 casted to the right type...
398     unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
399     uint64_t Val = ~0ULL;                // All ones
400     Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
401     return CU->getValue() == Val-1;
402   }
403
404   const ConstantSInt *CS = cast<ConstantSInt>(C);
405   
406   // Calculate 0111111111..11111
407   unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
408   int64_t Val = INT64_MAX;             // All ones
409   Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
410   return CS->getValue() == Val-1;
411 }
412
413 // isMinValuePlusOne - return true if this is Min+1
414 static bool isMinValuePlusOne(const ConstantInt *C) {
415   if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
416     return CU->getValue() == 1;
417
418   const ConstantSInt *CS = cast<ConstantSInt>(C);
419   
420   // Calculate 1111111111000000000000 
421   unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
422   int64_t Val = -1;                    // All ones
423   Val <<= TypeBits-1;                  // Shift over to the right spot
424   return CS->getValue() == Val+1;
425 }
426
427
428 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
429   bool Changed = SimplifyCommutative(I);
430   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
431
432   // and X, X = X   and X, 0 == 0
433   if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
434     return ReplaceInstUsesWith(I, Op1);
435
436   // and X, -1 == X
437   if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
438     if (RHS->isAllOnesValue())
439       return ReplaceInstUsesWith(I, Op0);
440
441   Value *Op0NotVal = dyn_castNotVal(Op0);
442   Value *Op1NotVal = dyn_castNotVal(Op1);
443
444   // (~A & ~B) == (~(A | B)) - Demorgan's Law
445   if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
446     Instruction *Or = BinaryOperator::create(Instruction::Or, Op0NotVal,
447                                              Op1NotVal,I.getName()+".demorgan",
448                                              &I);
449     WorkList.push_back(Or);
450     return BinaryOperator::createNot(Or);
451   }
452
453   if (Op0NotVal == Op1 || Op1NotVal == Op0)  // A & ~A  == ~A & A == 0
454     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
455
456   return Changed ? &I : 0;
457 }
458
459
460
461 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
462   bool Changed = SimplifyCommutative(I);
463   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
464
465   // or X, X = X   or X, 0 == X
466   if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
467     return ReplaceInstUsesWith(I, Op0);
468
469   // or X, -1 == -1
470   if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
471     if (RHS->isAllOnesValue())
472       return ReplaceInstUsesWith(I, Op1);
473
474   Value *Op0NotVal = dyn_castNotVal(Op0);
475   Value *Op1NotVal = dyn_castNotVal(Op1);
476
477   if (Op1 == Op0NotVal)   // ~A | A == -1
478     return ReplaceInstUsesWith(I, 
479                                ConstantIntegral::getAllOnesValue(I.getType()));
480
481   if (Op0 == Op1NotVal)   // A | ~A == -1
482     return ReplaceInstUsesWith(I, 
483                                ConstantIntegral::getAllOnesValue(I.getType()));
484
485   // (~A | ~B) == (~(A & B)) - Demorgan's Law
486   if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
487     Instruction *And = BinaryOperator::create(Instruction::And, Op0NotVal,
488                                               Op1NotVal,I.getName()+".demorgan",
489                                               &I);
490     WorkList.push_back(And);
491     return BinaryOperator::createNot(And);
492   }
493
494   return Changed ? &I : 0;
495 }
496
497
498
499 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
500   bool Changed = SimplifyCommutative(I);
501   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
502
503   // xor X, X = 0
504   if (Op0 == Op1)
505     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
506
507   if (ConstantIntegral *Op1C = dyn_cast<ConstantIntegral>(Op1)) {
508     // xor X, 0 == X
509     if (Op1C->isNullValue())
510       return ReplaceInstUsesWith(I, Op0);
511
512     // Is this a "NOT" instruction?
513     if (Op1C->isAllOnesValue()) {
514       // xor (xor X, -1), -1 = not (not X) = X
515       if (Value *X = dyn_castNotVal(Op0))
516         return ReplaceInstUsesWith(I, X);
517
518       // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
519       if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0))
520         if (SCI->use_size() == 1)
521           return new SetCondInst(SCI->getInverseCondition(),
522                                  SCI->getOperand(0), SCI->getOperand(1));
523     }
524   }
525
526   if (Value *X = dyn_castNotVal(Op0))   // ~A ^ A == -1
527     if (X == Op1)
528       return ReplaceInstUsesWith(I,
529                                 ConstantIntegral::getAllOnesValue(I.getType()));
530
531   if (Value *X = dyn_castNotVal(Op1))   // A ^ ~A == -1
532     if (X == Op0)
533       return ReplaceInstUsesWith(I,
534                                 ConstantIntegral::getAllOnesValue(I.getType()));
535
536
537
538   if (Instruction *Op1I = dyn_cast<Instruction>(Op1))
539     if (Op1I->getOpcode() == Instruction::Or)
540       if (Op1I->getOperand(0) == Op0) {              // B^(B|A) == (A|B)^B
541         cast<BinaryOperator>(Op1I)->swapOperands();
542         I.swapOperands();
543         std::swap(Op0, Op1);
544       } else if (Op1I->getOperand(1) == Op0) {       // B^(A|B) == (A|B)^B
545         I.swapOperands();
546         std::swap(Op0, Op1);
547       }
548
549   if (Instruction *Op0I = dyn_cast<Instruction>(Op0))
550     if (Op0I->getOpcode() == Instruction::Or && Op0I->use_size() == 1) {
551       if (Op0I->getOperand(0) == Op1)                // (B|A)^B == (A|B)^B
552         cast<BinaryOperator>(Op0I)->swapOperands();
553       if (Op0I->getOperand(1) == Op1) {              // (A|B)^B == A & ~B
554         Value *NotB = BinaryOperator::createNot(Op1, Op1->getName()+".not", &I);
555         WorkList.push_back(cast<Instruction>(NotB));
556         return BinaryOperator::create(Instruction::And, Op0I->getOperand(0),
557                                       NotB);
558       }
559     }
560
561   return Changed ? &I : 0;
562 }
563
564 // AddOne, SubOne - Add or subtract a constant one from an integer constant...
565 static Constant *AddOne(ConstantInt *C) {
566   Constant *Result = *C + *ConstantInt::get(C->getType(), 1);
567   assert(Result && "Constant folding integer addition failed!");
568   return Result;
569 }
570 static Constant *SubOne(ConstantInt *C) {
571   Constant *Result = *C - *ConstantInt::get(C->getType(), 1);
572   assert(Result && "Constant folding integer addition failed!");
573   return Result;
574 }
575
576 // isTrueWhenEqual - Return true if the specified setcondinst instruction is
577 // true when both operands are equal...
578 //
579 static bool isTrueWhenEqual(Instruction &I) {
580   return I.getOpcode() == Instruction::SetEQ ||
581          I.getOpcode() == Instruction::SetGE ||
582          I.getOpcode() == Instruction::SetLE;
583 }
584
585 Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
586   bool Changed = SimplifyCommutative(I);
587   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
588   const Type *Ty = Op0->getType();
589
590   // setcc X, X
591   if (Op0 == Op1)
592     return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
593
594   // setcc <global*>, 0 - Global value addresses are never null!
595   if (isa<GlobalValue>(Op0) && isa<ConstantPointerNull>(Op1))
596     return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
597
598   // setcc's with boolean values can always be turned into bitwise operations
599   if (Ty == Type::BoolTy) {
600     // If this is <, >, or !=, we can change this into a simple xor instruction
601     if (!isTrueWhenEqual(I))
602       return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName());
603
604     // Otherwise we need to make a temporary intermediate instruction and insert
605     // it into the instruction stream.  This is what we are after:
606     //
607     //  seteq bool %A, %B -> ~(A^B)
608     //  setle bool %A, %B -> ~A | B
609     //  setge bool %A, %B -> A | ~B
610     //
611     if (I.getOpcode() == Instruction::SetEQ) {  // seteq case
612       Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1,
613                                                 I.getName()+"tmp");
614       InsertNewInstBefore(Xor, I);
615       return BinaryOperator::createNot(Xor, I.getName());
616     }
617
618     // Handle the setXe cases...
619     assert(I.getOpcode() == Instruction::SetGE ||
620            I.getOpcode() == Instruction::SetLE);
621
622     if (I.getOpcode() == Instruction::SetGE)
623       std::swap(Op0, Op1);                   // Change setge -> setle
624
625     // Now we just have the SetLE case.
626     Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
627     InsertNewInstBefore(Not, I);
628     return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName());
629   }
630
631   // Check to see if we are doing one of many comparisons against constant
632   // integers at the end of their ranges...
633   //
634   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
635     // Check to see if we are comparing against the minimum or maximum value...
636     if (CI->isMinValue()) {
637       if (I.getOpcode() == Instruction::SetLT)       // A < MIN -> FALSE
638         return ReplaceInstUsesWith(I, ConstantBool::False);
639       if (I.getOpcode() == Instruction::SetGE)       // A >= MIN -> TRUE
640         return ReplaceInstUsesWith(I, ConstantBool::True);
641       if (I.getOpcode() == Instruction::SetLE)       // A <= MIN -> A == MIN
642         return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
643       if (I.getOpcode() == Instruction::SetGT)       // A > MIN -> A != MIN
644         return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
645
646     } else if (CI->isMaxValue()) {
647       if (I.getOpcode() == Instruction::SetGT)       // A > MAX -> FALSE
648         return ReplaceInstUsesWith(I, ConstantBool::False);
649       if (I.getOpcode() == Instruction::SetLE)       // A <= MAX -> TRUE
650         return ReplaceInstUsesWith(I, ConstantBool::True);
651       if (I.getOpcode() == Instruction::SetGE)       // A >= MAX -> A == MAX
652         return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
653       if (I.getOpcode() == Instruction::SetLT)       // A < MAX -> A != MAX
654         return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
655
656       // Comparing against a value really close to min or max?
657     } else if (isMinValuePlusOne(CI)) {
658       if (I.getOpcode() == Instruction::SetLT)       // A < MIN+1 -> A == MIN
659         return BinaryOperator::create(Instruction::SetEQ, Op0,
660                                       SubOne(CI), I.getName());
661       if (I.getOpcode() == Instruction::SetGE)       // A >= MIN-1 -> A != MIN
662         return BinaryOperator::create(Instruction::SetNE, Op0,
663                                       SubOne(CI), I.getName());
664
665     } else if (isMaxValueMinusOne(CI)) {
666       if (I.getOpcode() == Instruction::SetGT)       // A > MAX-1 -> A == MAX
667         return BinaryOperator::create(Instruction::SetEQ, Op0,
668                                       AddOne(CI), I.getName());
669       if (I.getOpcode() == Instruction::SetLE)       // A <= MAX-1 -> A != MAX
670         return BinaryOperator::create(Instruction::SetNE, Op0,
671                                       AddOne(CI), I.getName());
672     }
673   }
674
675   return Changed ? &I : 0;
676 }
677
678
679
680 Instruction *InstCombiner::visitShiftInst(ShiftInst &I) {
681   assert(I.getOperand(1)->getType() == Type::UByteTy);
682   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
683
684   // shl X, 0 == X and shr X, 0 == X
685   // shl 0, X == 0 and shr 0, X == 0
686   if (Op1 == Constant::getNullValue(Type::UByteTy) ||
687       Op0 == Constant::getNullValue(Op0->getType()))
688     return ReplaceInstUsesWith(I, Op0);
689
690   // If this is a shift of a shift, see if we can fold the two together...
691   if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) {
692     if (isa<Constant>(Op1) && isa<Constant>(Op0SI->getOperand(1))) {
693       ConstantUInt *ShiftAmt1C = cast<ConstantUInt>(Op0SI->getOperand(1));
694       unsigned ShiftAmt1 = ShiftAmt1C->getValue();
695       unsigned ShiftAmt2 = cast<ConstantUInt>(Op1)->getValue();
696
697       // Check for (A << c1) << c2   and   (A >> c1) >> c2
698       if (I.getOpcode() == Op0SI->getOpcode()) {
699         unsigned Amt = ShiftAmt1+ShiftAmt2;   // Fold into one big shift...
700         return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0),
701                              ConstantUInt::get(Type::UByteTy, Amt));
702       }
703
704       if (I.getType()->isUnsigned()) { // Check for (A << c1) >> c2 or visaversa
705         // Calculate bitmask for what gets shifted off the edge...
706         Constant *C = ConstantIntegral::getAllOnesValue(I.getType());
707         if (I.getOpcode() == Instruction::Shr)
708           C = *C >> *ShiftAmt1C;
709         else
710           C = *C << *ShiftAmt1C;
711         assert(C && "Couldn't constant fold shift expression?");
712           
713         Instruction *Mask =
714           BinaryOperator::create(Instruction::And, Op0SI->getOperand(0),
715                                  C, Op0SI->getOperand(0)->getName()+".mask",&I);
716         WorkList.push_back(Mask);
717           
718         // Figure out what flavor of shift we should use...
719         if (ShiftAmt1 == ShiftAmt2)
720           return ReplaceInstUsesWith(I, Mask);  // (A << c) >> c  === A & c2
721         else if (ShiftAmt1 < ShiftAmt2) {
722           return new ShiftInst(I.getOpcode(), Mask,
723                          ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1));
724         } else {
725           return new ShiftInst(Op0SI->getOpcode(), Mask,
726                          ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2));
727         }
728       }
729     }
730   }
731
732   // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr of
733   // a signed value.
734   //
735   if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
736     unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
737     if (CUI->getValue() >= TypeBits &&
738         (!Op0->getType()->isSigned() || I.getOpcode() == Instruction::Shl))
739       return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
740
741     // Check to see if we are shifting left by 1.  If so, turn it into an add
742     // instruction.
743     if (I.getOpcode() == Instruction::Shl && CUI->equalsInt(1))
744       // Convert 'shl int %X, 1' to 'add int %X, %X'
745       return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
746
747   }
748
749   // shr int -1, X = -1   (for any arithmetic shift rights of ~0)
750   if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
751     if (I.getOpcode() == Instruction::Shr && CSI->isAllOnesValue())
752       return ReplaceInstUsesWith(I, CSI);
753   
754   return 0;
755 }
756
757
758 // isEliminableCastOfCast - Return true if it is valid to eliminate the CI
759 // instruction.
760 //
761 static inline bool isEliminableCastOfCast(const CastInst &CI,
762                                           const CastInst *CSrc) {
763   assert(CI.getOperand(0) == CSrc);
764   const Type *SrcTy = CSrc->getOperand(0)->getType();
765   const Type *MidTy = CSrc->getType();
766   const Type *DstTy = CI.getType();
767
768   // It is legal to eliminate the instruction if casting A->B->A if the sizes
769   // are identical and the bits don't get reinterpreted (for example 
770   // int->float->int would not be allowed)
771   if (SrcTy == DstTy && SrcTy->isLosslesslyConvertableTo(MidTy))
772     return true;
773
774   // Allow free casting and conversion of sizes as long as the sign doesn't
775   // change...
776   if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
777     unsigned SrcSize = SrcTy->getPrimitiveSize();
778     unsigned MidSize = MidTy->getPrimitiveSize();
779     unsigned DstSize = DstTy->getPrimitiveSize();
780
781     // Cases where we are monotonically decreasing the size of the type are
782     // always ok, regardless of what sign changes are going on.
783     //
784     if (SrcSize >= MidSize && MidSize >= DstSize)
785       return true;
786
787     // Cases where the source and destination type are the same, but the middle
788     // type is bigger are noops.
789     //
790     if (SrcSize == DstSize && MidSize > SrcSize)
791       return true;
792
793     // If we are monotonically growing, things are more complex.
794     //
795     if (SrcSize <= MidSize && MidSize <= DstSize) {
796       // We have eight combinations of signedness to worry about. Here's the
797       // table:
798       static const int SignTable[8] = {
799         // CODE, SrcSigned, MidSigned, DstSigned, Comment
800         1,     //   U          U          U       Always ok
801         1,     //   U          U          S       Always ok
802         3,     //   U          S          U       Ok iff SrcSize != MidSize
803         3,     //   U          S          S       Ok iff SrcSize != MidSize
804         0,     //   S          U          U       Never ok
805         2,     //   S          U          S       Ok iff MidSize == DstSize
806         1,     //   S          S          U       Always ok
807         1,     //   S          S          S       Always ok
808       };
809
810       // Choose an action based on the current entry of the signtable that this
811       // cast of cast refers to...
812       unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned();
813       switch (SignTable[Row]) {
814       case 0: return false;              // Never ok
815       case 1: return true;               // Always ok
816       case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize
817       case 3:                            // Ok iff SrcSize != MidSize
818         return SrcSize != MidSize || SrcTy == Type::BoolTy;
819       default: assert(0 && "Bad entry in sign table!");
820       }
821     }
822   }
823
824   // Otherwise, we cannot succeed.  Specifically we do not want to allow things
825   // like:  short -> ushort -> uint, because this can create wrong results if
826   // the input short is negative!
827   //
828   return false;
829 }
830
831
832 // CastInst simplification
833 //
834 Instruction *InstCombiner::visitCastInst(CastInst &CI) {
835   // If the user is casting a value to the same type, eliminate this cast
836   // instruction...
837   if (CI.getType() == CI.getOperand(0)->getType())
838     return ReplaceInstUsesWith(CI, CI.getOperand(0));
839
840   // If casting the result of another cast instruction, try to eliminate this
841   // one!
842   //
843   if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) {
844     if (isEliminableCastOfCast(CI, CSrc)) {
845       // This instruction now refers directly to the cast's src operand.  This
846       // has a good chance of making CSrc dead.
847       CI.setOperand(0, CSrc->getOperand(0));
848       return &CI;
849     }
850
851     // If this is an A->B->A cast, and we are dealing with integral types, try
852     // to convert this into a logical 'and' instruction.
853     //
854     if (CSrc->getOperand(0)->getType() == CI.getType() &&
855         CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
856         CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
857         CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
858       assert(CSrc->getType() != Type::ULongTy &&
859              "Cannot have type bigger than ulong!");
860       unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1;
861       Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
862       return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
863                                     AndOp);
864     }
865   }
866
867   return 0;
868 }
869
870
871 // PHINode simplification
872 //
873 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
874   // If the PHI node only has one incoming value, eliminate the PHI node...
875   if (PN.getNumIncomingValues() == 1)
876     return ReplaceInstUsesWith(PN, PN.getIncomingValue(0));
877   
878   // Otherwise if all of the incoming values are the same for the PHI, replace
879   // the PHI node with the incoming value.
880   //
881   Value *InVal = 0;
882   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
883     if (PN.getIncomingValue(i) != &PN)  // Not the PHI node itself...
884       if (InVal && PN.getIncomingValue(i) != InVal)
885         return 0;  // Not the same, bail out.
886       else
887         InVal = PN.getIncomingValue(i);
888
889   // The only case that could cause InVal to be null is if we have a PHI node
890   // that only has entries for itself.  In this case, there is no entry into the
891   // loop, so kill the PHI.
892   //
893   if (InVal == 0) InVal = Constant::getNullValue(PN.getType());
894
895   // All of the incoming values are the same, replace the PHI node now.
896   return ReplaceInstUsesWith(PN, InVal);
897 }
898
899
900 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
901   // Is it 'getelementptr %P, uint 0'  or 'getelementptr %P'
902   // If so, eliminate the noop.
903   if ((GEP.getNumOperands() == 2 &&
904        GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) ||
905       GEP.getNumOperands() == 1)
906     return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
907
908   // Combine Indices - If the source pointer to this getelementptr instruction
909   // is a getelementptr instruction, combine the indices of the two
910   // getelementptr instructions into a single instruction.
911   //
912   if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) {
913     std::vector<Value *> Indices;
914   
915     // Can we combine the two pointer arithmetics offsets?
916      if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
917          isa<Constant>(GEP.getOperand(1))) {
918       // Replace: gep (gep %P, long C1), long C2, ...
919       // With:    gep %P, long (C1+C2), ...
920       Value *Sum = *cast<Constant>(Src->getOperand(1)) +
921                    *cast<Constant>(GEP.getOperand(1));
922       assert(Sum && "Constant folding of longs failed!?");
923       GEP.setOperand(0, Src->getOperand(0));
924       GEP.setOperand(1, Sum);
925       AddUsesToWorkList(*Src);   // Reduce use count of Src
926       return &GEP;
927     } else if (Src->getNumOperands() == 2 && Src->use_size() == 1) {
928       // Replace: gep (gep %P, long B), long A, ...
929       // With:    T = long A+B; gep %P, T, ...
930       //
931       Value *Sum = BinaryOperator::create(Instruction::Add, Src->getOperand(1),
932                                           GEP.getOperand(1),
933                                           Src->getName()+".sum", &GEP);
934       GEP.setOperand(0, Src->getOperand(0));
935       GEP.setOperand(1, Sum);
936       WorkList.push_back(cast<Instruction>(Sum));
937       return &GEP;
938     } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) &&
939                Src->getNumOperands() != 1) { 
940       // Otherwise we can do the fold if the first index of the GEP is a zero
941       Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
942       Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
943     } else if (Src->getOperand(Src->getNumOperands()-1) == 
944                Constant::getNullValue(Type::LongTy)) {
945       // If the src gep ends with a constant array index, merge this get into
946       // it, even if we have a non-zero array index.
947       Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()-1);
948       Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
949     }
950
951     if (!Indices.empty())
952       return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
953
954   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) {
955     // GEP of global variable.  If all of the indices for this GEP are
956     // constants, we can promote this to a constexpr instead of an instruction.
957
958     // Scan for nonconstants...
959     std::vector<Constant*> Indices;
960     User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
961     for (; I != E && isa<Constant>(*I); ++I)
962       Indices.push_back(cast<Constant>(*I));
963
964     if (I == E) {  // If they are all constants...
965       ConstantExpr *CE =
966         ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices);
967
968       // Replace all uses of the GEP with the new constexpr...
969       return ReplaceInstUsesWith(GEP, CE);
970     }
971   }
972
973   return 0;
974 }
975
976 Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
977   // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
978   if (AI.isArrayAllocation())    // Check C != 1
979     if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) {
980       const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue());
981       AllocationInst *New = 0;
982
983       // Create and insert the replacement instruction...
984       if (isa<MallocInst>(AI))
985         New = new MallocInst(NewTy, 0, AI.getName(), &AI);
986       else {
987         assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
988         New = new AllocaInst(NewTy, 0, AI.getName(), &AI);
989       }
990       
991       // Scan to the end of the allocation instructions, to skip over a block of
992       // allocas if possible...
993       //
994       BasicBlock::iterator It = New;
995       while (isa<AllocationInst>(*It)) ++It;
996
997       // Now that I is pointing to the first non-allocation-inst in the block,
998       // insert our getelementptr instruction...
999       //
1000       std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy));
1001       Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It);
1002
1003       // Now make everything use the getelementptr instead of the original
1004       // allocation.
1005       ReplaceInstUsesWith(AI, V);
1006       return &AI;
1007     }
1008   return 0;
1009 }
1010
1011
1012
1013 void InstCombiner::removeFromWorkList(Instruction *I) {
1014   WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
1015                  WorkList.end());
1016 }
1017
1018 bool InstCombiner::runOnFunction(Function &F) {
1019   bool Changed = false;
1020
1021   WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
1022
1023   while (!WorkList.empty()) {
1024     Instruction *I = WorkList.back();  // Get an instruction from the worklist
1025     WorkList.pop_back();
1026
1027     // Check to see if we can DCE or ConstantPropagate the instruction...
1028     // Check to see if we can DIE the instruction...
1029     if (isInstructionTriviallyDead(I)) {
1030       // Add operands to the worklist...
1031       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1032         if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
1033           WorkList.push_back(Op);
1034
1035       ++NumDeadInst;
1036       BasicBlock::iterator BBI = I;
1037       if (dceInstruction(BBI)) {
1038         removeFromWorkList(I);
1039         continue;
1040       }
1041     } 
1042
1043     // Instruction isn't dead, see if we can constant propagate it...
1044     if (Constant *C = ConstantFoldInstruction(I)) {
1045       // Add operands to the worklist...
1046       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1047         if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
1048           WorkList.push_back(Op);
1049       ReplaceInstUsesWith(*I, C);
1050
1051       ++NumConstProp;
1052       BasicBlock::iterator BBI = I;
1053       if (dceInstruction(BBI)) {
1054         removeFromWorkList(I);
1055         continue;
1056       }
1057     }
1058     
1059     // Now that we have an instruction, try combining it to simplify it...
1060     if (Instruction *Result = visit(*I)) {
1061       ++NumCombined;
1062       // Should we replace the old instruction with a new one?
1063       if (Result != I) {
1064         // Instructions can end up on the worklist more than once.  Make sure
1065         // we do not process an instruction that has been deleted.
1066         removeFromWorkList(I);
1067         ReplaceInstWithInst(I, Result);
1068       } else {
1069         BasicBlock::iterator II = I;
1070
1071         // If the instruction was modified, it's possible that it is now dead.
1072         // if so, remove it.
1073         if (dceInstruction(II)) {
1074           // Instructions may end up in the worklist more than once.  Erase them
1075           // all.
1076           removeFromWorkList(I);
1077           Result = 0;
1078         }
1079       }
1080
1081       if (Result) {
1082         WorkList.push_back(Result);
1083         AddUsesToWorkList(*Result);
1084       }
1085       Changed = true;
1086     }
1087   }
1088
1089   return Changed;
1090 }
1091
1092 Pass *createInstructionCombiningPass() {
1093   return new InstCombiner();
1094 }