LLVM  3.7.0
InstructionCombining.cpp
Go to the documentation of this file.
1 //===- InstructionCombining.cpp - Combine multiple instructions -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // InstructionCombining - Combine instructions to form fewer, simple
11 // instructions. This pass does not modify the CFG. This pass is where
12 // algebraic simplification happens.
13 //
14 // This pass combines things like:
15 // %Y = add i32 %X, 1
16 // %Z = add i32 %Y, 1
17 // into:
18 // %Z = add i32 %X, 2
19 //
20 // This is a simple worklist driven algorithm.
21 //
22 // This pass guarantees that the following canonicalizations are performed on
23 // the program:
24 // 1. If a binary operator has a constant operand, it is moved to the RHS
25 // 2. Bitwise operators with constant operands are always grouped so that
26 // shifts are performed first, then or's, then and's, then xor's.
27 // 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
28 // 4. All cmp instructions on boolean values are replaced with logical ops
29 // 5. add X, X is represented as (X*2) => (X << 1)
30 // 6. Multiplies with a power-of-two constant argument are transformed into
31 // shifts.
32 // ... etc.
33 //
34 //===----------------------------------------------------------------------===//
35 
37 #include "InstCombineInternal.h"
38 #include "llvm-c/Initialization.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include "llvm/ADT/Statistic.h"
41 #include "llvm/ADT/StringSwitch.h"
43 #include "llvm/Analysis/CFG.h"
47 #include "llvm/Analysis/LoopInfo.h"
51 #include "llvm/IR/CFG.h"
52 #include "llvm/IR/DataLayout.h"
53 #include "llvm/IR/Dominators.h"
55 #include "llvm/IR/IntrinsicInst.h"
56 #include "llvm/IR/PatternMatch.h"
57 #include "llvm/IR/ValueHandle.h"
59 #include "llvm/Support/Debug.h"
61 #include "llvm/Transforms/Scalar.h"
63 #include <algorithm>
64 #include <climits>
65 using namespace llvm;
66 using namespace llvm::PatternMatch;
67 
68 #define DEBUG_TYPE "instcombine"
69 
70 STATISTIC(NumCombined , "Number of insts combined");
71 STATISTIC(NumConstProp, "Number of constant folds");
72 STATISTIC(NumDeadInst , "Number of dead inst eliminated");
73 STATISTIC(NumSunkInst , "Number of instructions sunk");
74 STATISTIC(NumExpand, "Number of expansions");
75 STATISTIC(NumFactor , "Number of factorizations");
76 STATISTIC(NumReassoc , "Number of reassociations");
77 
79  return llvm::EmitGEPOffset(Builder, DL, GEP);
80 }
81 
82 /// ShouldChangeType - Return true if it is desirable to convert a computation
83 /// from 'From' to 'To'. We don't want to convert from a legal to an illegal
84 /// type for example, or from a smaller to a larger illegal type.
85 bool InstCombiner::ShouldChangeType(Type *From, Type *To) const {
86  assert(From->isIntegerTy() && To->isIntegerTy());
87 
88  unsigned FromWidth = From->getPrimitiveSizeInBits();
89  unsigned ToWidth = To->getPrimitiveSizeInBits();
90  bool FromLegal = DL.isLegalInteger(FromWidth);
91  bool ToLegal = DL.isLegalInteger(ToWidth);
92 
93  // If this is a legal integer from type, and the result would be an illegal
94  // type, don't do the transformation.
95  if (FromLegal && !ToLegal)
96  return false;
97 
98  // Otherwise, if both are illegal, do not increase the size of the result. We
99  // do allow things like i160 -> i64, but not i64 -> i160.
100  if (!FromLegal && !ToLegal && ToWidth > FromWidth)
101  return false;
102 
103  return true;
104 }
105 
106 // Return true, if No Signed Wrap should be maintained for I.
107 // The No Signed Wrap flag can be kept if the operation "B (I.getOpcode) C",
108 // where both B and C should be ConstantInts, results in a constant that does
109 // not overflow. This function only handles the Add and Sub opcodes. For
110 // all other opcodes, the function conservatively returns false.
113  if (!OBO || !OBO->hasNoSignedWrap()) {
114  return false;
115  }
116 
117  // We reason about Add and Sub Only.
118  Instruction::BinaryOps Opcode = I.getOpcode();
119  if (Opcode != Instruction::Add &&
120  Opcode != Instruction::Sub) {
121  return false;
122  }
123 
124  ConstantInt *CB = dyn_cast<ConstantInt>(B);
126 
127  if (!CB || !CC) {
128  return false;
129  }
130 
131  const APInt &BVal = CB->getValue();
132  const APInt &CVal = CC->getValue();
133  bool Overflow = false;
134 
135  if (Opcode == Instruction::Add) {
136  BVal.sadd_ov(CVal, Overflow);
137  } else {
138  BVal.ssub_ov(CVal, Overflow);
139  }
140 
141  return !Overflow;
142 }
143 
144 /// Conservatively clears subclassOptionalData after a reassociation or
145 /// commutation. We preserve fast-math flags when applicable as they can be
146 /// preserved.
149  if (!FPMO) {
151  return;
152  }
153 
156  I.setFastMathFlags(FMF);
157 }
158 
159 /// SimplifyAssociativeOrCommutative - This performs a few simplifications for
160 /// operators which are associative or commutative:
161 //
162 // Commutative operators:
163 //
164 // 1. Order operands such that they are listed from right (least complex) to
165 // left (most complex). This puts constants before unary operators before
166 // binary operators.
167 //
168 // Associative operators:
169 //
170 // 2. Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies.
171 // 3. Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies.
172 //
173 // Associative and commutative operators:
174 //
175 // 4. Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies.
176 // 5. Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies.
177 // 6. Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)"
178 // if C1 and C2 are constants.
179 //
180 bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
181  Instruction::BinaryOps Opcode = I.getOpcode();
182  bool Changed = false;
183 
184  do {
185  // Order operands such that they are listed from right (least complex) to
186  // left (most complex). This puts constants before unary operators before
187  // binary operators.
188  if (I.isCommutative() && getComplexity(I.getOperand(0)) <
190  Changed = !I.swapOperands();
191 
194 
195  if (I.isAssociative()) {
196  // Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies.
197  if (Op0 && Op0->getOpcode() == Opcode) {
198  Value *A = Op0->getOperand(0);
199  Value *B = Op0->getOperand(1);
200  Value *C = I.getOperand(1);
201 
202  // Does "B op C" simplify?
203  if (Value *V = SimplifyBinOp(Opcode, B, C, DL)) {
204  // It simplifies to V. Form "A op V".
205  I.setOperand(0, A);
206  I.setOperand(1, V);
207  // Conservatively clear the optional flags, since they may not be
208  // preserved by the reassociation.
209  if (MaintainNoSignedWrap(I, B, C) &&
210  (!Op0 || (isa<BinaryOperator>(Op0) && Op0->hasNoSignedWrap()))) {
211  // Note: this is only valid because SimplifyBinOp doesn't look at
212  // the operands to Op0.
214  I.setHasNoSignedWrap(true);
215  } else {
217  }
218 
219  Changed = true;
220  ++NumReassoc;
221  continue;
222  }
223  }
224 
225  // Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies.
226  if (Op1 && Op1->getOpcode() == Opcode) {
227  Value *A = I.getOperand(0);
228  Value *B = Op1->getOperand(0);
229  Value *C = Op1->getOperand(1);
230 
231  // Does "A op B" simplify?
232  if (Value *V = SimplifyBinOp(Opcode, A, B, DL)) {
233  // It simplifies to V. Form "V op C".
234  I.setOperand(0, V);
235  I.setOperand(1, C);
236  // Conservatively clear the optional flags, since they may not be
237  // preserved by the reassociation.
239  Changed = true;
240  ++NumReassoc;
241  continue;
242  }
243  }
244  }
245 
246  if (I.isAssociative() && I.isCommutative()) {
247  // Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies.
248  if (Op0 && Op0->getOpcode() == Opcode) {
249  Value *A = Op0->getOperand(0);
250  Value *B = Op0->getOperand(1);
251  Value *C = I.getOperand(1);
252 
253  // Does "C op A" simplify?
254  if (Value *V = SimplifyBinOp(Opcode, C, A, DL)) {
255  // It simplifies to V. Form "V op B".
256  I.setOperand(0, V);
257  I.setOperand(1, B);
258  // Conservatively clear the optional flags, since they may not be
259  // preserved by the reassociation.
261  Changed = true;
262  ++NumReassoc;
263  continue;
264  }
265  }
266 
267  // Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies.
268  if (Op1 && Op1->getOpcode() == Opcode) {
269  Value *A = I.getOperand(0);
270  Value *B = Op1->getOperand(0);
271  Value *C = Op1->getOperand(1);
272 
273  // Does "C op A" simplify?
274  if (Value *V = SimplifyBinOp(Opcode, C, A, DL)) {
275  // It simplifies to V. Form "B op V".
276  I.setOperand(0, B);
277  I.setOperand(1, V);
278  // Conservatively clear the optional flags, since they may not be
279  // preserved by the reassociation.
281  Changed = true;
282  ++NumReassoc;
283  continue;
284  }
285  }
286 
287  // Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)"
288  // if C1 and C2 are constants.
289  if (Op0 && Op1 &&
290  Op0->getOpcode() == Opcode && Op1->getOpcode() == Opcode &&
291  isa<Constant>(Op0->getOperand(1)) &&
292  isa<Constant>(Op1->getOperand(1)) &&
293  Op0->hasOneUse() && Op1->hasOneUse()) {
294  Value *A = Op0->getOperand(0);
295  Constant *C1 = cast<Constant>(Op0->getOperand(1));
296  Value *B = Op1->getOperand(0);
297  Constant *C2 = cast<Constant>(Op1->getOperand(1));
298 
299  Constant *Folded = ConstantExpr::get(Opcode, C1, C2);
300  BinaryOperator *New = BinaryOperator::Create(Opcode, A, B);
301  if (isa<FPMathOperator>(New)) {
303  Flags &= Op0->getFastMathFlags();
304  Flags &= Op1->getFastMathFlags();
305  New->setFastMathFlags(Flags);
306  }
307  InsertNewInstWith(New, I);
308  New->takeName(Op1);
309  I.setOperand(0, New);
310  I.setOperand(1, Folded);
311  // Conservatively clear the optional flags, since they may not be
312  // preserved by the reassociation.
314 
315  Changed = true;
316  continue;
317  }
318  }
319 
320  // No further simplifications.
321  return Changed;
322  } while (1);
323 }
324 
325 /// LeftDistributesOverRight - Whether "X LOp (Y ROp Z)" is always equal to
326 /// "(X LOp Y) ROp (X LOp Z)".
329  switch (LOp) {
330  default:
331  return false;
332 
333  case Instruction::And:
334  // And distributes over Or and Xor.
335  switch (ROp) {
336  default:
337  return false;
338  case Instruction::Or:
339  case Instruction::Xor:
340  return true;
341  }
342 
343  case Instruction::Mul:
344  // Multiplication distributes over addition and subtraction.
345  switch (ROp) {
346  default:
347  return false;
348  case Instruction::Add:
349  case Instruction::Sub:
350  return true;
351  }
352 
353  case Instruction::Or:
354  // Or distributes over And.
355  switch (ROp) {
356  default:
357  return false;
358  case Instruction::And:
359  return true;
360  }
361  }
362 }
363 
364 /// RightDistributesOverLeft - Whether "(X LOp Y) ROp Z" is always equal to
365 /// "(X ROp Z) LOp (Y ROp Z)".
369  return LeftDistributesOverRight(ROp, LOp);
370 
371  switch (LOp) {
372  default:
373  return false;
374  // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts.
375  // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts.
376  // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts.
377  case Instruction::And:
378  case Instruction::Or:
379  case Instruction::Xor:
380  switch (ROp) {
381  default:
382  return false;
383  case Instruction::Shl:
384  case Instruction::LShr:
385  case Instruction::AShr:
386  return true;
387  }
388  }
389  // TODO: It would be nice to handle division, aka "(X + Y)/Z = X/Z + Y/Z",
390  // but this requires knowing that the addition does not overflow and other
391  // such subtleties.
392  return false;
393 }
394 
395 /// This function returns identity value for given opcode, which can be used to
396 /// factor patterns like (X * 2) + X ==> (X * 2) + (X * 1) ==> X * (2 + 1).
398  if (isa<Constant>(V))
399  return nullptr;
400 
401  if (OpCode == Instruction::Mul)
402  return ConstantInt::get(V->getType(), 1);
403 
404  // TODO: We can handle other cases e.g. Instruction::And, Instruction::Or etc.
405 
406  return nullptr;
407 }
408 
409 /// This function factors binary ops which can be combined using distributive
410 /// laws. This function tries to transform 'Op' based TopLevelOpcode to enable
411 /// factorization e.g for ADD(SHL(X , 2), MUL(X, 5)), When this function called
412 /// with TopLevelOpcode == Instruction::Add and Op = SHL(X, 2), transforms
413 /// SHL(X, 2) to MUL(X, 4) i.e. returns Instruction::Mul with LHS set to 'X' and
414 /// RHS to 4.
417  BinaryOperator *Op, Value *&LHS, Value *&RHS) {
418  if (!Op)
419  return Instruction::BinaryOpsEnd;
420 
421  LHS = Op->getOperand(0);
422  RHS = Op->getOperand(1);
423 
424  switch (TopLevelOpcode) {
425  default:
426  return Op->getOpcode();
427 
428  case Instruction::Add:
429  case Instruction::Sub:
430  if (Op->getOpcode() == Instruction::Shl) {
431  if (Constant *CST = dyn_cast<Constant>(Op->getOperand(1))) {
432  // The multiplier is really 1 << CST.
433  RHS = ConstantExpr::getShl(ConstantInt::get(Op->getType(), 1), CST);
434  return Instruction::Mul;
435  }
436  }
437  return Op->getOpcode();
438  }
439 
440  // TODO: We can add other conversions e.g. shr => div etc.
441 }
442 
443 /// This tries to simplify binary operations by factorizing out common terms
444 /// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
446  const DataLayout &DL, BinaryOperator &I,
447  Instruction::BinaryOps InnerOpcode, Value *A,
448  Value *B, Value *C, Value *D) {
449 
450  // If any of A, B, C, D are null, we can not factor I, return early.
451  // Checking A and C should be enough.
452  if (!A || !C || !B || !D)
453  return nullptr;
454 
455  Value *V = nullptr;
456  Value *SimplifiedInst = nullptr;
457  Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
458  Instruction::BinaryOps TopLevelOpcode = I.getOpcode();
459 
460  // Does "X op' Y" always equal "Y op' X"?
461  bool InnerCommutative = Instruction::isCommutative(InnerOpcode);
462 
463  // Does "X op' (Y op Z)" always equal "(X op' Y) op (X op' Z)"?
464  if (LeftDistributesOverRight(InnerOpcode, TopLevelOpcode))
465  // Does the instruction have the form "(A op' B) op (A op' D)" or, in the
466  // commutative case, "(A op' B) op (C op' A)"?
467  if (A == C || (InnerCommutative && A == D)) {
468  if (A != C)
469  std::swap(C, D);
470  // Consider forming "A op' (B op D)".
471  // If "B op D" simplifies then it can be formed with no cost.
472  V = SimplifyBinOp(TopLevelOpcode, B, D, DL);
473  // If "B op D" doesn't simplify then only go on if both of the existing
474  // operations "A op' B" and "C op' D" will be zapped as no longer used.
475  if (!V && LHS->hasOneUse() && RHS->hasOneUse())
476  V = Builder->CreateBinOp(TopLevelOpcode, B, D, RHS->getName());
477  if (V) {
478  SimplifiedInst = Builder->CreateBinOp(InnerOpcode, A, V);
479  }
480  }
481 
482  // Does "(X op Y) op' Z" always equal "(X op' Z) op (Y op' Z)"?
483  if (!SimplifiedInst && RightDistributesOverLeft(TopLevelOpcode, InnerOpcode))
484  // Does the instruction have the form "(A op' B) op (C op' B)" or, in the
485  // commutative case, "(A op' B) op (B op' D)"?
486  if (B == D || (InnerCommutative && B == C)) {
487  if (B != D)
488  std::swap(C, D);
489  // Consider forming "(A op C) op' B".
490  // If "A op C" simplifies then it can be formed with no cost.
491  V = SimplifyBinOp(TopLevelOpcode, A, C, DL);
492 
493  // If "A op C" doesn't simplify then only go on if both of the existing
494  // operations "A op' B" and "C op' D" will be zapped as no longer used.
495  if (!V && LHS->hasOneUse() && RHS->hasOneUse())
496  V = Builder->CreateBinOp(TopLevelOpcode, A, C, LHS->getName());
497  if (V) {
498  SimplifiedInst = Builder->CreateBinOp(InnerOpcode, V, B);
499  }
500  }
501 
502  if (SimplifiedInst) {
503  ++NumFactor;
504  SimplifiedInst->takeName(&I);
505 
506  // Check if we can add NSW flag to SimplifiedInst. If so, set NSW flag.
507  // TODO: Check for NUW.
508  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(SimplifiedInst)) {
509  if (isa<OverflowingBinaryOperator>(SimplifiedInst)) {
510  bool HasNSW = false;
511  if (isa<OverflowingBinaryOperator>(&I))
512  HasNSW = I.hasNoSignedWrap();
513 
514  if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS))
515  if (isa<OverflowingBinaryOperator>(Op0))
516  HasNSW &= Op0->hasNoSignedWrap();
517 
518  if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS))
519  if (isa<OverflowingBinaryOperator>(Op1))
520  HasNSW &= Op1->hasNoSignedWrap();
521 
522  // We can propogate 'nsw' if we know that
523  // %Y = mul nsw i16 %X, C
524  // %Z = add nsw i16 %Y, %X
525  // =>
526  // %Z = mul nsw i16 %X, C+1
527  //
528  // iff C+1 isn't INT_MIN
529  const APInt *CInt;
530  if (TopLevelOpcode == Instruction::Add &&
531  InnerOpcode == Instruction::Mul)
532  if (match(V, m_APInt(CInt)) && !CInt->isMinSignedValue())
533  BO->setHasNoSignedWrap(HasNSW);
534  }
535  }
536  }
537  return SimplifiedInst;
538 }
539 
540 /// SimplifyUsingDistributiveLaws - This tries to simplify binary operations
541 /// which some other binary operation distributes over either by factorizing
542 /// out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this
543 /// results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is
544 /// a win). Returns the simplified value, or null if it didn't simplify.
545 Value *InstCombiner::SimplifyUsingDistributiveLaws(BinaryOperator &I) {
546  Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
549 
550  // Factorization.
551  Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
552  auto TopLevelOpcode = I.getOpcode();
553  auto LHSOpcode = getBinOpsForFactorization(TopLevelOpcode, Op0, A, B);
554  auto RHSOpcode = getBinOpsForFactorization(TopLevelOpcode, Op1, C, D);
555 
556  // The instruction has the form "(A op' B) op (C op' D)". Try to factorize
557  // a common term.
558  if (LHSOpcode == RHSOpcode) {
559  if (Value *V = tryFactorization(Builder, DL, I, LHSOpcode, A, B, C, D))
560  return V;
561  }
562 
563  // The instruction has the form "(A op' B) op (C)". Try to factorize common
564  // term.
565  if (Value *V = tryFactorization(Builder, DL, I, LHSOpcode, A, B, RHS,
566  getIdentityValue(LHSOpcode, RHS)))
567  return V;
568 
569  // The instruction has the form "(B) op (C op' D)". Try to factorize common
570  // term.
571  if (Value *V = tryFactorization(Builder, DL, I, RHSOpcode, LHS,
572  getIdentityValue(RHSOpcode, LHS), C, D))
573  return V;
574 
575  // Expansion.
576  if (Op0 && RightDistributesOverLeft(Op0->getOpcode(), TopLevelOpcode)) {
577  // The instruction has the form "(A op' B) op C". See if expanding it out
578  // to "(A op C) op' (B op C)" results in simplifications.
579  Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
580  Instruction::BinaryOps InnerOpcode = Op0->getOpcode(); // op'
581 
582  // Do "A op C" and "B op C" both simplify?
583  if (Value *L = SimplifyBinOp(TopLevelOpcode, A, C, DL))
584  if (Value *R = SimplifyBinOp(TopLevelOpcode, B, C, DL)) {
585  // They do! Return "L op' R".
586  ++NumExpand;
587  // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
588  if ((L == A && R == B) ||
589  (Instruction::isCommutative(InnerOpcode) && L == B && R == A))
590  return Op0;
591  // Otherwise return "L op' R" if it simplifies.
592  if (Value *V = SimplifyBinOp(InnerOpcode, L, R, DL))
593  return V;
594  // Otherwise, create a new instruction.
595  C = Builder->CreateBinOp(InnerOpcode, L, R);
596  C->takeName(&I);
597  return C;
598  }
599  }
600 
601  if (Op1 && LeftDistributesOverRight(TopLevelOpcode, Op1->getOpcode())) {
602  // The instruction has the form "A op (B op' C)". See if expanding it out
603  // to "(A op B) op' (A op C)" results in simplifications.
604  Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
605  Instruction::BinaryOps InnerOpcode = Op1->getOpcode(); // op'
606 
607  // Do "A op B" and "A op C" both simplify?
608  if (Value *L = SimplifyBinOp(TopLevelOpcode, A, B, DL))
609  if (Value *R = SimplifyBinOp(TopLevelOpcode, A, C, DL)) {
610  // They do! Return "L op' R".
611  ++NumExpand;
612  // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
613  if ((L == B && R == C) ||
614  (Instruction::isCommutative(InnerOpcode) && L == C && R == B))
615  return Op1;
616  // Otherwise return "L op' R" if it simplifies.
617  if (Value *V = SimplifyBinOp(InnerOpcode, L, R, DL))
618  return V;
619  // Otherwise, create a new instruction.
620  A = Builder->CreateBinOp(InnerOpcode, L, R);
621  A->takeName(&I);
622  return A;
623  }
624  }
625 
626  return nullptr;
627 }
628 
629 // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
630 // if the LHS is a constant zero (which is the 'negate' form).
631 //
632 Value *InstCombiner::dyn_castNegVal(Value *V) const {
633  if (BinaryOperator::isNeg(V))
635 
636  // Constants can be considered to be negated values if they can be folded.
637  if (ConstantInt *C = dyn_cast<ConstantInt>(V))
638  return ConstantExpr::getNeg(C);
639 
640  if (ConstantDataVector *C = dyn_cast<ConstantDataVector>(V))
641  if (C->getType()->getElementType()->isIntegerTy())
642  return ConstantExpr::getNeg(C);
643 
644  return nullptr;
645 }
646 
647 // dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the
648 // instruction if the LHS is a constant negative zero (which is the 'negate'
649 // form).
650 //
651 Value *InstCombiner::dyn_castFNegVal(Value *V, bool IgnoreZeroSign) const {
652  if (BinaryOperator::isFNeg(V, IgnoreZeroSign))
654 
655  // Constants can be considered to be negated values if they can be folded.
656  if (ConstantFP *C = dyn_cast<ConstantFP>(V))
657  return ConstantExpr::getFNeg(C);
658 
659  if (ConstantDataVector *C = dyn_cast<ConstantDataVector>(V))
660  if (C->getType()->getElementType()->isFloatingPointTy())
661  return ConstantExpr::getFNeg(C);
662 
663  return nullptr;
664 }
665 
667  InstCombiner *IC) {
668  if (CastInst *CI = dyn_cast<CastInst>(&I)) {
669  return IC->Builder->CreateCast(CI->getOpcode(), SO, I.getType());
670  }
671 
672  // Figure out if the constant is the left or the right argument.
673  bool ConstIsRHS = isa<Constant>(I.getOperand(1));
674  Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
675 
676  if (Constant *SOC = dyn_cast<Constant>(SO)) {
677  if (ConstIsRHS)
678  return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
679  return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
680  }
681 
682  Value *Op0 = SO, *Op1 = ConstOperand;
683  if (!ConstIsRHS)
684  std::swap(Op0, Op1);
685 
686  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) {
687  Value *RI = IC->Builder->CreateBinOp(BO->getOpcode(), Op0, Op1,
688  SO->getName()+".op");
689  Instruction *FPInst = dyn_cast<Instruction>(RI);
690  if (FPInst && isa<FPMathOperator>(FPInst))
691  FPInst->copyFastMathFlags(BO);
692  return RI;
693  }
694  if (ICmpInst *CI = dyn_cast<ICmpInst>(&I))
695  return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
696  SO->getName()+".cmp");
697  if (FCmpInst *CI = dyn_cast<FCmpInst>(&I))
698  return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
699  SO->getName()+".cmp");
700  llvm_unreachable("Unknown binary instruction type!");
701 }
702 
703 // FoldOpIntoSelect - Given an instruction with a select as one operand and a
704 // constant as the other operand, try to fold the binary operator into the
705 // select arguments. This also works for Cast instructions, which obviously do
706 // not have a second operand.
707 Instruction *InstCombiner::FoldOpIntoSelect(Instruction &Op, SelectInst *SI) {
708  // Don't modify shared select instructions
709  if (!SI->hasOneUse()) return nullptr;
710  Value *TV = SI->getOperand(1);
711  Value *FV = SI->getOperand(2);
712 
713  if (isa<Constant>(TV) || isa<Constant>(FV)) {
714  // Bool selects with constant operands can be folded to logical ops.
715  if (SI->getType()->isIntegerTy(1)) return nullptr;
716 
717  // If it's a bitcast involving vectors, make sure it has the same number of
718  // elements on both sides.
719  if (BitCastInst *BC = dyn_cast<BitCastInst>(&Op)) {
720  VectorType *DestTy = dyn_cast<VectorType>(BC->getDestTy());
721  VectorType *SrcTy = dyn_cast<VectorType>(BC->getSrcTy());
722 
723  // Verify that either both or neither are vectors.
724  if ((SrcTy == nullptr) != (DestTy == nullptr)) return nullptr;
725  // If vectors, verify that they have the same number of elements.
726  if (SrcTy && SrcTy->getNumElements() != DestTy->getNumElements())
727  return nullptr;
728  }
729 
730  // Test if a CmpInst instruction is used exclusively by a select as
731  // part of a minimum or maximum operation. If so, refrain from doing
732  // any other folding. This helps out other analyses which understand
733  // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
734  // and CodeGen. And in this case, at least one of the comparison
735  // operands has at least one user besides the compare (the select),
736  // which would often largely negate the benefit of folding anyway.
737  if (auto *CI = dyn_cast<CmpInst>(SI->getCondition())) {
738  if (CI->hasOneUse()) {
739  Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
740  if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
741  (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
742  return nullptr;
743  }
744  }
745 
746  Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, this);
747  Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, this);
748 
749  return SelectInst::Create(SI->getCondition(),
750  SelectTrueVal, SelectFalseVal);
751  }
752  return nullptr;
753 }
754 
755 /// FoldOpIntoPhi - Given a binary operator, cast instruction, or select which
756 /// has a PHI node as operand #0, see if we can fold the instruction into the
757 /// PHI (which is only possible if all operands to the PHI are constants).
758 ///
759 Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
760  PHINode *PN = cast<PHINode>(I.getOperand(0));
761  unsigned NumPHIValues = PN->getNumIncomingValues();
762  if (NumPHIValues == 0)
763  return nullptr;
764 
765  // We normally only transform phis with a single use. However, if a PHI has
766  // multiple uses and they are all the same operation, we can fold *all* of the
767  // uses into the PHI.
768  if (!PN->hasOneUse()) {
769  // Walk the use list for the instruction, comparing them to I.
770  for (User *U : PN->users()) {
771  Instruction *UI = cast<Instruction>(U);
772  if (UI != &I && !I.isIdenticalTo(UI))
773  return nullptr;
774  }
775  // Otherwise, we can replace *all* users with the new PHI we form.
776  }
777 
778  // Check to see if all of the operands of the PHI are simple constants
779  // (constantint/constantfp/undef). If there is one non-constant value,
780  // remember the BB it is in. If there is more than one or if *it* is a PHI,
781  // bail out. We don't do arbitrary constant expressions here because moving
782  // their computation can be expensive without a cost model.
783  BasicBlock *NonConstBB = nullptr;
784  for (unsigned i = 0; i != NumPHIValues; ++i) {
785  Value *InVal = PN->getIncomingValue(i);
786  if (isa<Constant>(InVal) && !isa<ConstantExpr>(InVal))
787  continue;
788 
789  if (isa<PHINode>(InVal)) return nullptr; // Itself a phi.
790  if (NonConstBB) return nullptr; // More than one non-const value.
791 
792  NonConstBB = PN->getIncomingBlock(i);
793 
794  // If the InVal is an invoke at the end of the pred block, then we can't
795  // insert a computation after it without breaking the edge.
796  if (InvokeInst *II = dyn_cast<InvokeInst>(InVal))
797  if (II->getParent() == NonConstBB)
798  return nullptr;
799 
800  // If the incoming non-constant value is in I's block, we will remove one
801  // instruction, but insert another equivalent one, leading to infinite
802  // instcombine.
803  if (isPotentiallyReachable(I.getParent(), NonConstBB, DT, LI))
804  return nullptr;
805  }
806 
807  // If there is exactly one non-constant value, we can insert a copy of the
808  // operation in that block. However, if this is a critical edge, we would be
809  // inserting the computation on some other paths (e.g. inside a loop). Only
810  // do this if the pred block is unconditionally branching into the phi block.
811  if (NonConstBB != nullptr) {
812  BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
813  if (!BI || !BI->isUnconditional()) return nullptr;
814  }
815 
816  // Okay, we can do the transformation: create the new PHI node.
818  InsertNewInstBefore(NewPN, *PN);
819  NewPN->takeName(PN);
820 
821  // If we are going to have to insert a new computation, do so right before the
822  // predecessors terminator.
823  if (NonConstBB)
824  Builder->SetInsertPoint(NonConstBB->getTerminator());
825 
826  // Next, add all of the operands to the PHI.
827  if (SelectInst *SI = dyn_cast<SelectInst>(&I)) {
828  // We only currently try to fold the condition of a select when it is a phi,
829  // not the true/false values.
830  Value *TrueV = SI->getTrueValue();
831  Value *FalseV = SI->getFalseValue();
832  BasicBlock *PhiTransBB = PN->getParent();
833  for (unsigned i = 0; i != NumPHIValues; ++i) {
834  BasicBlock *ThisBB = PN->getIncomingBlock(i);
835  Value *TrueVInPred = TrueV->DoPHITranslation(PhiTransBB, ThisBB);
836  Value *FalseVInPred = FalseV->DoPHITranslation(PhiTransBB, ThisBB);
837  Value *InV = nullptr;
838  // Beware of ConstantExpr: it may eventually evaluate to getNullValue,
839  // even if currently isNullValue gives false.
840  Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i));
841  if (InC && !isa<ConstantExpr>(InC))
842  InV = InC->isNullValue() ? FalseVInPred : TrueVInPred;
843  else
844  InV = Builder->CreateSelect(PN->getIncomingValue(i),
845  TrueVInPred, FalseVInPred, "phitmp");
846  NewPN->addIncoming(InV, ThisBB);
847  }
848  } else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) {
849  Constant *C = cast<Constant>(I.getOperand(1));
850  for (unsigned i = 0; i != NumPHIValues; ++i) {
851  Value *InV = nullptr;
852  if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
853  InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
854  else if (isa<ICmpInst>(CI))
855  InV = Builder->CreateICmp(CI->getPredicate(), PN->getIncomingValue(i),
856  C, "phitmp");
857  else
858  InV = Builder->CreateFCmp(CI->getPredicate(), PN->getIncomingValue(i),
859  C, "phitmp");
860  NewPN->addIncoming(InV, PN->getIncomingBlock(i));
861  }
862  } else if (I.getNumOperands() == 2) {
863  Constant *C = cast<Constant>(I.getOperand(1));
864  for (unsigned i = 0; i != NumPHIValues; ++i) {
865  Value *InV = nullptr;
866  if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
867  InV = ConstantExpr::get(I.getOpcode(), InC, C);
868  else
869  InV = Builder->CreateBinOp(cast<BinaryOperator>(I).getOpcode(),
870  PN->getIncomingValue(i), C, "phitmp");
871  NewPN->addIncoming(InV, PN->getIncomingBlock(i));
872  }
873  } else {
874  CastInst *CI = cast<CastInst>(&I);
875  Type *RetTy = CI->getType();
876  for (unsigned i = 0; i != NumPHIValues; ++i) {
877  Value *InV;
878  if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
879  InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
880  else
881  InV = Builder->CreateCast(CI->getOpcode(),
882  PN->getIncomingValue(i), I.getType(), "phitmp");
883  NewPN->addIncoming(InV, PN->getIncomingBlock(i));
884  }
885  }
886 
887  for (auto UI = PN->user_begin(), E = PN->user_end(); UI != E;) {
888  Instruction *User = cast<Instruction>(*UI++);
889  if (User == &I) continue;
890  ReplaceInstUsesWith(*User, NewPN);
891  EraseInstFromFunction(*User);
892  }
893  return ReplaceInstUsesWith(I, NewPN);
894 }
895 
896 /// FindElementAtOffset - Given a pointer type and a constant offset, determine
897 /// whether or not there is a sequence of GEP indices into the pointed type that
898 /// will land us at the specified offset. If so, fill them into NewIndices and
899 /// return the resultant element type, otherwise return null.
900 Type *InstCombiner::FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
901  SmallVectorImpl<Value *> &NewIndices) {
902  Type *Ty = PtrTy->getElementType();
903  if (!Ty->isSized())
904  return nullptr;
905 
906  // Start with the index over the outer type. Note that the type size
907  // might be zero (even if the offset isn't zero) if the indexed type
908  // is something like [0 x {int, int}]
909  Type *IntPtrTy = DL.getIntPtrType(PtrTy);
910  int64_t FirstIdx = 0;
911  if (int64_t TySize = DL.getTypeAllocSize(Ty)) {
912  FirstIdx = Offset/TySize;
913  Offset -= FirstIdx*TySize;
914 
915  // Handle hosts where % returns negative instead of values [0..TySize).
916  if (Offset < 0) {
917  --FirstIdx;
918  Offset += TySize;
919  assert(Offset >= 0);
920  }
921  assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
922  }
923 
924  NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
925 
926  // Index into the types. If we fail, set OrigBase to null.
927  while (Offset) {
928  // Indexing into tail padding between struct/array elements.
929  if (uint64_t(Offset * 8) >= DL.getTypeSizeInBits(Ty))
930  return nullptr;
931 
932  if (StructType *STy = dyn_cast<StructType>(Ty)) {
933  const StructLayout *SL = DL.getStructLayout(STy);
934  assert(Offset < (int64_t)SL->getSizeInBytes() &&
935  "Offset must stay within the indexed type");
936 
937  unsigned Elt = SL->getElementContainingOffset(Offset);
939  Elt));
940 
941  Offset -= SL->getElementOffset(Elt);
942  Ty = STy->getElementType(Elt);
943  } else if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
944  uint64_t EltSize = DL.getTypeAllocSize(AT->getElementType());
945  assert(EltSize && "Cannot index into a zero-sized array");
946  NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
947  Offset %= EltSize;
948  Ty = AT->getElementType();
949  } else {
950  // Otherwise, we can't index into the middle of this atomic type, bail.
951  return nullptr;
952  }
953  }
954 
955  return Ty;
956 }
957 
959  // If this GEP has only 0 indices, it is the same pointer as
960  // Src. If Src is not a trivial GEP too, don't combine
961  // the indices.
962  if (GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() &&
963  !Src.hasOneUse())
964  return false;
965  return true;
966 }
967 
968 /// Descale - Return a value X such that Val = X * Scale, or null if none. If
969 /// the multiplication is known not to overflow then NoSignedWrap is set.
970 Value *InstCombiner::Descale(Value *Val, APInt Scale, bool &NoSignedWrap) {
971  assert(isa<IntegerType>(Val->getType()) && "Can only descale integers!");
972  assert(cast<IntegerType>(Val->getType())->getBitWidth() ==
973  Scale.getBitWidth() && "Scale not compatible with value!");
974 
975  // If Val is zero or Scale is one then Val = Val * Scale.
976  if (match(Val, m_Zero()) || Scale == 1) {
977  NoSignedWrap = true;
978  return Val;
979  }
980 
981  // If Scale is zero then it does not divide Val.
982  if (Scale.isMinValue())
983  return nullptr;
984 
985  // Look through chains of multiplications, searching for a constant that is
986  // divisible by Scale. For example, descaling X*(Y*(Z*4)) by a factor of 4
987  // will find the constant factor 4 and produce X*(Y*Z). Descaling X*(Y*8) by
988  // a factor of 4 will produce X*(Y*2). The principle of operation is to bore
989  // down from Val:
990  //
991  // Val = M1 * X || Analysis starts here and works down
992  // M1 = M2 * Y || Doesn't descend into terms with more
993  // M2 = Z * 4 \/ than one use
994  //
995  // Then to modify a term at the bottom:
996  //
997  // Val = M1 * X
998  // M1 = Z * Y || Replaced M2 with Z
999  //
1000  // Then to work back up correcting nsw flags.
1001 
1002  // Op - the term we are currently analyzing. Starts at Val then drills down.
1003  // Replaced with its descaled value before exiting from the drill down loop.
1004  Value *Op = Val;
1005 
1006  // Parent - initially null, but after drilling down notes where Op came from.
1007  // In the example above, Parent is (Val, 0) when Op is M1, because M1 is the
1008  // 0'th operand of Val.
1009  std::pair<Instruction*, unsigned> Parent;
1010 
1011  // RequireNoSignedWrap - Set if the transform requires a descaling at deeper
1012  // levels that doesn't overflow.
1013  bool RequireNoSignedWrap = false;
1014 
1015  // logScale - log base 2 of the scale. Negative if not a power of 2.
1016  int32_t logScale = Scale.exactLogBase2();
1017 
1018  for (;; Op = Parent.first->getOperand(Parent.second)) { // Drill down
1019 
1020  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1021  // If Op is a constant divisible by Scale then descale to the quotient.
1022  APInt Quotient(Scale), Remainder(Scale); // Init ensures right bitwidth.
1023  APInt::sdivrem(CI->getValue(), Scale, Quotient, Remainder);
1024  if (!Remainder.isMinValue())
1025  // Not divisible by Scale.
1026  return nullptr;
1027  // Replace with the quotient in the parent.
1028  Op = ConstantInt::get(CI->getType(), Quotient);
1029  NoSignedWrap = true;
1030  break;
1031  }
1032 
1033  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op)) {
1034 
1035  if (BO->getOpcode() == Instruction::Mul) {
1036  // Multiplication.
1037  NoSignedWrap = BO->hasNoSignedWrap();
1038  if (RequireNoSignedWrap && !NoSignedWrap)
1039  return nullptr;
1040 
1041  // There are three cases for multiplication: multiplication by exactly
1042  // the scale, multiplication by a constant different to the scale, and
1043  // multiplication by something else.
1044  Value *LHS = BO->getOperand(0);
1045  Value *RHS = BO->getOperand(1);
1046 
1047  if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1048  // Multiplication by a constant.
1049  if (CI->getValue() == Scale) {
1050  // Multiplication by exactly the scale, replace the multiplication
1051  // by its left-hand side in the parent.
1052  Op = LHS;
1053  break;
1054  }
1055 
1056  // Otherwise drill down into the constant.
1057  if (!Op->hasOneUse())
1058  return nullptr;
1059 
1060  Parent = std::make_pair(BO, 1);
1061  continue;
1062  }
1063 
1064  // Multiplication by something else. Drill down into the left-hand side
1065  // since that's where the reassociate pass puts the good stuff.
1066  if (!Op->hasOneUse())
1067  return nullptr;
1068 
1069  Parent = std::make_pair(BO, 0);
1070  continue;
1071  }
1072 
1073  if (logScale > 0 && BO->getOpcode() == Instruction::Shl &&
1074  isa<ConstantInt>(BO->getOperand(1))) {
1075  // Multiplication by a power of 2.
1076  NoSignedWrap = BO->hasNoSignedWrap();
1077  if (RequireNoSignedWrap && !NoSignedWrap)
1078  return nullptr;
1079 
1080  Value *LHS = BO->getOperand(0);
1081  int32_t Amt = cast<ConstantInt>(BO->getOperand(1))->
1082  getLimitedValue(Scale.getBitWidth());
1083  // Op = LHS << Amt.
1084 
1085  if (Amt == logScale) {
1086  // Multiplication by exactly the scale, replace the multiplication
1087  // by its left-hand side in the parent.
1088  Op = LHS;
1089  break;
1090  }
1091  if (Amt < logScale || !Op->hasOneUse())
1092  return nullptr;
1093 
1094  // Multiplication by more than the scale. Reduce the multiplying amount
1095  // by the scale in the parent.
1096  Parent = std::make_pair(BO, 1);
1097  Op = ConstantInt::get(BO->getType(), Amt - logScale);
1098  break;
1099  }
1100  }
1101 
1102  if (!Op->hasOneUse())
1103  return nullptr;
1104 
1105  if (CastInst *Cast = dyn_cast<CastInst>(Op)) {
1106  if (Cast->getOpcode() == Instruction::SExt) {
1107  // Op is sign-extended from a smaller type, descale in the smaller type.
1108  unsigned SmallSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
1109  APInt SmallScale = Scale.trunc(SmallSize);
1110  // Suppose Op = sext X, and we descale X as Y * SmallScale. We want to
1111  // descale Op as (sext Y) * Scale. In order to have
1112  // sext (Y * SmallScale) = (sext Y) * Scale
1113  // some conditions need to hold however: SmallScale must sign-extend to
1114  // Scale and the multiplication Y * SmallScale should not overflow.
1115  if (SmallScale.sext(Scale.getBitWidth()) != Scale)
1116  // SmallScale does not sign-extend to Scale.
1117  return nullptr;
1118  assert(SmallScale.exactLogBase2() == logScale);
1119  // Require that Y * SmallScale must not overflow.
1120  RequireNoSignedWrap = true;
1121 
1122  // Drill down through the cast.
1123  Parent = std::make_pair(Cast, 0);
1124  Scale = SmallScale;
1125  continue;
1126  }
1127 
1128  if (Cast->getOpcode() == Instruction::Trunc) {
1129  // Op is truncated from a larger type, descale in the larger type.
1130  // Suppose Op = trunc X, and we descale X as Y * sext Scale. Then
1131  // trunc (Y * sext Scale) = (trunc Y) * Scale
1132  // always holds. However (trunc Y) * Scale may overflow even if
1133  // trunc (Y * sext Scale) does not, so nsw flags need to be cleared
1134  // from this point up in the expression (see later).
1135  if (RequireNoSignedWrap)
1136  return nullptr;
1137 
1138  // Drill down through the cast.
1139  unsigned LargeSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
1140  Parent = std::make_pair(Cast, 0);
1141  Scale = Scale.sext(LargeSize);
1142  if (logScale + 1 == (int32_t)Cast->getType()->getPrimitiveSizeInBits())
1143  logScale = -1;
1144  assert(Scale.exactLogBase2() == logScale);
1145  continue;
1146  }
1147  }
1148 
1149  // Unsupported expression, bail out.
1150  return nullptr;
1151  }
1152 
1153  // If Op is zero then Val = Op * Scale.
1154  if (match(Op, m_Zero())) {
1155  NoSignedWrap = true;
1156  return Op;
1157  }
1158 
1159  // We know that we can successfully descale, so from here on we can safely
1160  // modify the IR. Op holds the descaled version of the deepest term in the
1161  // expression. NoSignedWrap is 'true' if multiplying Op by Scale is known
1162  // not to overflow.
1163 
1164  if (!Parent.first)
1165  // The expression only had one term.
1166  return Op;
1167 
1168  // Rewrite the parent using the descaled version of its operand.
1169  assert(Parent.first->hasOneUse() && "Drilled down when more than one use!");
1170  assert(Op != Parent.first->getOperand(Parent.second) &&
1171  "Descaling was a no-op?");
1172  Parent.first->setOperand(Parent.second, Op);
1173  Worklist.Add(Parent.first);
1174 
1175  // Now work back up the expression correcting nsw flags. The logic is based
1176  // on the following observation: if X * Y is known not to overflow as a signed
1177  // multiplication, and Y is replaced by a value Z with smaller absolute value,
1178  // then X * Z will not overflow as a signed multiplication either. As we work
1179  // our way up, having NoSignedWrap 'true' means that the descaled value at the
1180  // current level has strictly smaller absolute value than the original.
1181  Instruction *Ancestor = Parent.first;
1182  do {
1183  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Ancestor)) {
1184  // If the multiplication wasn't nsw then we can't say anything about the
1185  // value of the descaled multiplication, and we have to clear nsw flags
1186  // from this point on up.
1187  bool OpNoSignedWrap = BO->hasNoSignedWrap();
1188  NoSignedWrap &= OpNoSignedWrap;
1189  if (NoSignedWrap != OpNoSignedWrap) {
1190  BO->setHasNoSignedWrap(NoSignedWrap);
1191  Worklist.Add(Ancestor);
1192  }
1193  } else if (Ancestor->getOpcode() == Instruction::Trunc) {
1194  // The fact that the descaled input to the trunc has smaller absolute
1195  // value than the original input doesn't tell us anything useful about
1196  // the absolute values of the truncations.
1197  NoSignedWrap = false;
1198  }
1199  assert((Ancestor->getOpcode() != Instruction::SExt || NoSignedWrap) &&
1200  "Failed to keep proper track of nsw flags while drilling down?");
1201 
1202  if (Ancestor == Val)
1203  // Got to the top, all done!
1204  return Val;
1205 
1206  // Move up one level in the expression.
1207  assert(Ancestor->hasOneUse() && "Drilled down when more than one use!");
1208  Ancestor = Ancestor->user_back();
1209  } while (1);
1210 }
1211 
1212 /// \brief Creates node of binary operation with the same attributes as the
1213 /// specified one but with other operands.
1216  Value *BORes = B->CreateBinOp(Inst.getOpcode(), LHS, RHS);
1217  if (BinaryOperator *NewBO = dyn_cast<BinaryOperator>(BORes)) {
1218  if (isa<OverflowingBinaryOperator>(NewBO)) {
1219  NewBO->setHasNoSignedWrap(Inst.hasNoSignedWrap());
1220  NewBO->setHasNoUnsignedWrap(Inst.hasNoUnsignedWrap());
1221  }
1222  if (isa<PossiblyExactOperator>(NewBO))
1223  NewBO->setIsExact(Inst.isExact());
1224  }
1225  return BORes;
1226 }
1227 
1228 /// \brief Makes transformation of binary operation specific for vector types.
1229 /// \param Inst Binary operator to transform.
1230 /// \return Pointer to node that must replace the original binary operator, or
1231 /// null pointer if no transformation was made.
1232 Value *InstCombiner::SimplifyVectorOp(BinaryOperator &Inst) {
1233  if (!Inst.getType()->isVectorTy()) return nullptr;
1234 
1235  // It may not be safe to reorder shuffles and things like div, urem, etc.
1236  // because we may trap when executing those ops on unknown vector elements.
1237  // See PR20059.
1238  if (!isSafeToSpeculativelyExecute(&Inst))
1239  return nullptr;
1240 
1241  unsigned VWidth = cast<VectorType>(Inst.getType())->getNumElements();
1242  Value *LHS = Inst.getOperand(0), *RHS = Inst.getOperand(1);
1243  assert(cast<VectorType>(LHS->getType())->getNumElements() == VWidth);
1244  assert(cast<VectorType>(RHS->getType())->getNumElements() == VWidth);
1245 
1246  // If both arguments of binary operation are shuffles, which use the same
1247  // mask and shuffle within a single vector, it is worthwhile to move the
1248  // shuffle after binary operation:
1249  // Op(shuffle(v1, m), shuffle(v2, m)) -> shuffle(Op(v1, v2), m)
1250  if (isa<ShuffleVectorInst>(LHS) && isa<ShuffleVectorInst>(RHS)) {
1251  ShuffleVectorInst *LShuf = cast<ShuffleVectorInst>(LHS);
1252  ShuffleVectorInst *RShuf = cast<ShuffleVectorInst>(RHS);
1253  if (isa<UndefValue>(LShuf->getOperand(1)) &&
1254  isa<UndefValue>(RShuf->getOperand(1)) &&
1255  LShuf->getOperand(0)->getType() == RShuf->getOperand(0)->getType() &&
1256  LShuf->getMask() == RShuf->getMask()) {
1257  Value *NewBO = CreateBinOpAsGiven(Inst, LShuf->getOperand(0),
1258  RShuf->getOperand(0), Builder);
1259  Value *Res = Builder->CreateShuffleVector(NewBO,
1260  UndefValue::get(NewBO->getType()), LShuf->getMask());
1261  return Res;
1262  }
1263  }
1264 
1265  // If one argument is a shuffle within one vector, the other is a constant,
1266  // try moving the shuffle after the binary operation.
1267  ShuffleVectorInst *Shuffle = nullptr;
1268  Constant *C1 = nullptr;
1269  if (isa<ShuffleVectorInst>(LHS)) Shuffle = cast<ShuffleVectorInst>(LHS);
1270  if (isa<ShuffleVectorInst>(RHS)) Shuffle = cast<ShuffleVectorInst>(RHS);
1271  if (isa<Constant>(LHS)) C1 = cast<Constant>(LHS);
1272  if (isa<Constant>(RHS)) C1 = cast<Constant>(RHS);
1273  if (Shuffle && C1 &&
1274  (isa<ConstantVector>(C1) || isa<ConstantDataVector>(C1)) &&
1275  isa<UndefValue>(Shuffle->getOperand(1)) &&
1276  Shuffle->getType() == Shuffle->getOperand(0)->getType()) {
1277  SmallVector<int, 16> ShMask = Shuffle->getShuffleMask();
1278  // Find constant C2 that has property:
1279  // shuffle(C2, ShMask) = C1
1280  // If such constant does not exist (example: ShMask=<0,0> and C1=<1,2>)
1281  // reorder is not possible.
1282  SmallVector<Constant*, 16> C2M(VWidth,
1284  bool MayChange = true;
1285  for (unsigned I = 0; I < VWidth; ++I) {
1286  if (ShMask[I] >= 0) {
1287  assert(ShMask[I] < (int)VWidth);
1288  if (!isa<UndefValue>(C2M[ShMask[I]])) {
1289  MayChange = false;
1290  break;
1291  }
1292  C2M[ShMask[I]] = C1->getAggregateElement(I);
1293  }
1294  }
1295  if (MayChange) {
1296  Constant *C2 = ConstantVector::get(C2M);
1297  Value *NewLHS, *NewRHS;
1298  if (isa<Constant>(LHS)) {
1299  NewLHS = C2;
1300  NewRHS = Shuffle->getOperand(0);
1301  } else {
1302  NewLHS = Shuffle->getOperand(0);
1303  NewRHS = C2;
1304  }
1305  Value *NewBO = CreateBinOpAsGiven(Inst, NewLHS, NewRHS, Builder);
1306  Value *Res = Builder->CreateShuffleVector(NewBO,
1307  UndefValue::get(Inst.getType()), Shuffle->getMask());
1308  return Res;
1309  }
1310  }
1311 
1312  return nullptr;
1313 }
1314 
1316  SmallVector<Value*, 8> Ops(GEP.op_begin(), GEP.op_end());
1317 
1318  if (Value *V = SimplifyGEPInst(Ops, DL, TLI, DT, AC))
1319  return ReplaceInstUsesWith(GEP, V);
1320 
1321  Value *PtrOp = GEP.getOperand(0);
1322 
1323  // Eliminate unneeded casts for indices, and replace indices which displace
1324  // by multiples of a zero size type with zero.
1325  bool MadeChange = false;
1326  Type *IntPtrTy = DL.getIntPtrType(GEP.getPointerOperandType());
1327 
1328  gep_type_iterator GTI = gep_type_begin(GEP);
1329  for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end(); I != E;
1330  ++I, ++GTI) {
1331  // Skip indices into struct types.
1332  SequentialType *SeqTy = dyn_cast<SequentialType>(*GTI);
1333  if (!SeqTy)
1334  continue;
1335 
1336  // If the element type has zero size then any index over it is equivalent
1337  // to an index of zero, so replace it with zero if it is not zero already.
1338  if (SeqTy->getElementType()->isSized() &&
1339  DL.getTypeAllocSize(SeqTy->getElementType()) == 0)
1340  if (!isa<Constant>(*I) || !cast<Constant>(*I)->isNullValue()) {
1341  *I = Constant::getNullValue(IntPtrTy);
1342  MadeChange = true;
1343  }
1344 
1345  Type *IndexTy = (*I)->getType();
1346  if (IndexTy != IntPtrTy) {
1347  // If we are using a wider index than needed for this platform, shrink
1348  // it to what we need. If narrower, sign-extend it to what we need.
1349  // This explicit cast can make subsequent optimizations more obvious.
1350  *I = Builder->CreateIntCast(*I, IntPtrTy, true);
1351  MadeChange = true;
1352  }
1353  }
1354  if (MadeChange)
1355  return &GEP;
1356 
1357  // Check to see if the inputs to the PHI node are getelementptr instructions.
1358  if (PHINode *PN = dyn_cast<PHINode>(PtrOp)) {
1360  if (!Op1)
1361  return nullptr;
1362 
1363  // Don't fold a GEP into itself through a PHI node. This can only happen
1364  // through the back-edge of a loop. Folding a GEP into itself means that
1365  // the value of the previous iteration needs to be stored in the meantime,
1366  // thus requiring an additional register variable to be live, but not
1367  // actually achieving anything (the GEP still needs to be executed once per
1368  // loop iteration).
1369  if (Op1 == &GEP)
1370  return nullptr;
1371 
1372  signed DI = -1;
1373 
1374  for (auto I = PN->op_begin()+1, E = PN->op_end(); I !=E; ++I) {
1376  if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands())
1377  return nullptr;
1378 
1379  // As for Op1 above, don't try to fold a GEP into itself.
1380  if (Op2 == &GEP)
1381  return nullptr;
1382 
1383  // Keep track of the type as we walk the GEP.
1384  Type *CurTy = Op1->getOperand(0)->getType()->getScalarType();
1385 
1386  for (unsigned J = 0, F = Op1->getNumOperands(); J != F; ++J) {
1387  if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())
1388  return nullptr;
1389 
1390  if (Op1->getOperand(J) != Op2->getOperand(J)) {
1391  if (DI == -1) {
1392  // We have not seen any differences yet in the GEPs feeding the
1393  // PHI yet, so we record this one if it is allowed to be a
1394  // variable.
1395 
1396  // The first two arguments can vary for any GEP, the rest have to be
1397  // static for struct slots
1398  if (J > 1 && CurTy->isStructTy())
1399  return nullptr;
1400 
1401  DI = J;
1402  } else {
1403  // The GEP is different by more than one input. While this could be
1404  // extended to support GEPs that vary by more than one variable it
1405  // doesn't make sense since it greatly increases the complexity and
1406  // would result in an R+R+R addressing mode which no backend
1407  // directly supports and would need to be broken into several
1408  // simpler instructions anyway.
1409  return nullptr;
1410  }
1411  }
1412 
1413  // Sink down a layer of the type for the next iteration.
1414  if (J > 0) {
1415  if (CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
1416  CurTy = CT->getTypeAtIndex(Op1->getOperand(J));
1417  } else {
1418  CurTy = nullptr;
1419  }
1420  }
1421  }
1422  }
1423 
1424  GetElementPtrInst *NewGEP = cast<GetElementPtrInst>(Op1->clone());
1425 
1426  if (DI == -1) {
1427  // All the GEPs feeding the PHI are identical. Clone one down into our
1428  // BB so that it can be merged with the current GEP.
1429  GEP.getParent()->getInstList().insert(
1430  GEP.getParent()->getFirstInsertionPt(), NewGEP);
1431  } else {
1432  // All the GEPs feeding the PHI differ at a single offset. Clone a GEP
1433  // into the current block so it can be merged, and create a new PHI to
1434  // set that index.
1435  Instruction *InsertPt = Builder->GetInsertPoint();
1436  Builder->SetInsertPoint(PN);
1437  PHINode *NewPN = Builder->CreatePHI(Op1->getOperand(DI)->getType(),
1438  PN->getNumOperands());
1439  Builder->SetInsertPoint(InsertPt);
1440 
1441  for (auto &I : PN->operands())
1442  NewPN->addIncoming(cast<GEPOperator>(I)->getOperand(DI),
1443  PN->getIncomingBlock(I));
1444 
1445  NewGEP->setOperand(DI, NewPN);
1446  GEP.getParent()->getInstList().insert(
1447  GEP.getParent()->getFirstInsertionPt(), NewGEP);
1448  NewGEP->setOperand(DI, NewPN);
1449  }
1450 
1451  GEP.setOperand(0, NewGEP);
1452  PtrOp = NewGEP;
1453  }
1454 
1455  // Combine Indices - If the source pointer to this getelementptr instruction
1456  // is a getelementptr instruction, combine the indices of the two
1457  // getelementptr instructions into a single instruction.
1458  //
1459  if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) {
1460  if (!shouldMergeGEPs(*cast<GEPOperator>(&GEP), *Src))
1461  return nullptr;
1462 
1463  // Note that if our source is a gep chain itself then we wait for that
1464  // chain to be resolved before we perform this transformation. This
1465  // avoids us creating a TON of code in some cases.
1466  if (GEPOperator *SrcGEP =
1467  dyn_cast<GEPOperator>(Src->getOperand(0)))
1468  if (SrcGEP->getNumOperands() == 2 && shouldMergeGEPs(*Src, *SrcGEP))
1469  return nullptr; // Wait until our source is folded to completion.
1470 
1471  SmallVector<Value*, 8> Indices;
1472 
1473  // Find out whether the last index in the source GEP is a sequential idx.
1474  bool EndsWithSequential = false;
1475  for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src);
1476  I != E; ++I)
1477  EndsWithSequential = !(*I)->isStructTy();
1478 
1479  // Can we combine the two pointer arithmetics offsets?
1480  if (EndsWithSequential) {
1481  // Replace: gep (gep %P, long B), long A, ...
1482  // With: T = long A+B; gep %P, T, ...
1483  //
1484  Value *Sum;
1485  Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
1486  Value *GO1 = GEP.getOperand(1);
1487  if (SO1 == Constant::getNullValue(SO1->getType())) {
1488  Sum = GO1;
1489  } else if (GO1 == Constant::getNullValue(GO1->getType())) {
1490  Sum = SO1;
1491  } else {
1492  // If they aren't the same type, then the input hasn't been processed
1493  // by the loop above yet (which canonicalizes sequential index types to
1494  // intptr_t). Just avoid transforming this until the input has been
1495  // normalized.
1496  if (SO1->getType() != GO1->getType())
1497  return nullptr;
1498  // Only do the combine when GO1 and SO1 are both constants. Only in
1499  // this case, we are sure the cost after the merge is never more than
1500  // that before the merge.
1501  if (!isa<Constant>(GO1) || !isa<Constant>(SO1))
1502  return nullptr;
1503  Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
1504  }
1505 
1506  // Update the GEP in place if possible.
1507  if (Src->getNumOperands() == 2) {
1508  GEP.setOperand(0, Src->getOperand(0));
1509  GEP.setOperand(1, Sum);
1510  return &GEP;
1511  }
1512  Indices.append(Src->op_begin()+1, Src->op_end()-1);
1513  Indices.push_back(Sum);
1514  Indices.append(GEP.op_begin()+2, GEP.op_end());
1515  } else if (isa<Constant>(*GEP.idx_begin()) &&
1516  cast<Constant>(*GEP.idx_begin())->isNullValue() &&
1517  Src->getNumOperands() != 1) {
1518  // Otherwise we can do the fold if the first index of the GEP is a zero
1519  Indices.append(Src->op_begin()+1, Src->op_end());
1520  Indices.append(GEP.idx_begin()+1, GEP.idx_end());
1521  }
1522 
1523  if (!Indices.empty())
1524  return GEP.isInBounds() && Src->isInBounds()
1526  Src->getSourceElementType(), Src->getOperand(0), Indices,
1527  GEP.getName())
1528  : GetElementPtrInst::Create(Src->getSourceElementType(),
1529  Src->getOperand(0), Indices,
1530  GEP.getName());
1531  }
1532 
1533  if (GEP.getNumIndices() == 1) {
1534  unsigned AS = GEP.getPointerAddressSpace();
1535  if (GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
1536  DL.getPointerSizeInBits(AS)) {
1537  Type *PtrTy = GEP.getPointerOperandType();
1538  Type *Ty = PtrTy->getPointerElementType();
1539  uint64_t TyAllocSize = DL.getTypeAllocSize(Ty);
1540 
1541  bool Matched = false;
1542  uint64_t C;
1543  Value *V = nullptr;
1544  if (TyAllocSize == 1) {
1545  V = GEP.getOperand(1);
1546  Matched = true;
1547  } else if (match(GEP.getOperand(1),
1548  m_AShr(m_Value(V), m_ConstantInt(C)))) {
1549  if (TyAllocSize == 1ULL << C)
1550  Matched = true;
1551  } else if (match(GEP.getOperand(1),
1552  m_SDiv(m_Value(V), m_ConstantInt(C)))) {
1553  if (TyAllocSize == C)
1554  Matched = true;
1555  }
1556 
1557  if (Matched) {
1558  // Canonicalize (gep i8* X, -(ptrtoint Y))
1559  // to (inttoptr (sub (ptrtoint X), (ptrtoint Y)))
1560  // The GEP pattern is emitted by the SCEV expander for certain kinds of
1561  // pointer arithmetic.
1562  if (match(V, m_Neg(m_PtrToInt(m_Value())))) {
1563  Operator *Index = cast<Operator>(V);
1564  Value *PtrToInt = Builder->CreatePtrToInt(PtrOp, Index->getType());
1565  Value *NewSub = Builder->CreateSub(PtrToInt, Index->getOperand(1));
1566  return CastInst::Create(Instruction::IntToPtr, NewSub, GEP.getType());
1567  }
1568  // Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X))
1569  // to (bitcast Y)
1570  Value *Y;
1571  if (match(V, m_Sub(m_PtrToInt(m_Value(Y)),
1572  m_PtrToInt(m_Specific(GEP.getOperand(0)))))) {
1574  GEP.getType());
1575  }
1576  }
1577  }
1578  }
1579 
1580  // Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
1581  Value *StrippedPtr = PtrOp->stripPointerCasts();
1582  PointerType *StrippedPtrTy = dyn_cast<PointerType>(StrippedPtr->getType());
1583 
1584  // We do not handle pointer-vector geps here.
1585  if (!StrippedPtrTy)
1586  return nullptr;
1587 
1588  if (StrippedPtr != PtrOp) {
1589  bool HasZeroPointerIndex = false;
1590  if (ConstantInt *C = dyn_cast<ConstantInt>(GEP.getOperand(1)))
1591  HasZeroPointerIndex = C->isZero();
1592 
1593  // Transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
1594  // into : GEP [10 x i8]* X, i32 0, ...
1595  //
1596  // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
1597  // into : GEP i8* X, ...
1598  //
1599  // This occurs when the program declares an array extern like "int X[];"
1600  if (HasZeroPointerIndex) {
1601  PointerType *CPTy = cast<PointerType>(PtrOp->getType());
1602  if (ArrayType *CATy =
1603  dyn_cast<ArrayType>(CPTy->getElementType())) {
1604  // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
1605  if (CATy->getElementType() == StrippedPtrTy->getElementType()) {
1606  // -> GEP i8* X, ...
1607  SmallVector<Value*, 8> Idx(GEP.idx_begin()+1, GEP.idx_end());
1609  StrippedPtrTy->getElementType(), StrippedPtr, Idx, GEP.getName());
1610  Res->setIsInBounds(GEP.isInBounds());
1611  if (StrippedPtrTy->getAddressSpace() == GEP.getAddressSpace())
1612  return Res;
1613  // Insert Res, and create an addrspacecast.
1614  // e.g.,
1615  // GEP (addrspacecast i8 addrspace(1)* X to [0 x i8]*), i32 0, ...
1616  // ->
1617  // %0 = GEP i8 addrspace(1)* X, ...
1618  // addrspacecast i8 addrspace(1)* %0 to i8*
1619  return new AddrSpaceCastInst(Builder->Insert(Res), GEP.getType());
1620  }
1621 
1622  if (ArrayType *XATy =
1623  dyn_cast<ArrayType>(StrippedPtrTy->getElementType())){
1624  // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
1625  if (CATy->getElementType() == XATy->getElementType()) {
1626  // -> GEP [10 x i8]* X, i32 0, ...
1627  // At this point, we know that the cast source type is a pointer
1628  // to an array of the same type as the destination pointer
1629  // array. Because the array type is never stepped over (there
1630  // is a leading zero) we can fold the cast into this GEP.
1631  if (StrippedPtrTy->getAddressSpace() == GEP.getAddressSpace()) {
1632  GEP.setOperand(0, StrippedPtr);
1633  GEP.setSourceElementType(XATy);
1634  return &GEP;
1635  }
1636  // Cannot replace the base pointer directly because StrippedPtr's
1637  // address space is different. Instead, create a new GEP followed by
1638  // an addrspacecast.
1639  // e.g.,
1640  // GEP (addrspacecast [10 x i8] addrspace(1)* X to [0 x i8]*),
1641  // i32 0, ...
1642  // ->
1643  // %0 = GEP [10 x i8] addrspace(1)* X, ...
1644  // addrspacecast i8 addrspace(1)* %0 to i8*
1645  SmallVector<Value*, 8> Idx(GEP.idx_begin(), GEP.idx_end());
1646  Value *NewGEP = GEP.isInBounds()
1647  ? Builder->CreateInBoundsGEP(
1648  nullptr, StrippedPtr, Idx, GEP.getName())
1649  : Builder->CreateGEP(nullptr, StrippedPtr, Idx,
1650  GEP.getName());
1651  return new AddrSpaceCastInst(NewGEP, GEP.getType());
1652  }
1653  }
1654  }
1655  } else if (GEP.getNumOperands() == 2) {
1656  // Transform things like:
1657  // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
1658  // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
1659  Type *SrcElTy = StrippedPtrTy->getElementType();
1660  Type *ResElTy = PtrOp->getType()->getPointerElementType();
1661  if (SrcElTy->isArrayTy() &&
1662  DL.getTypeAllocSize(SrcElTy->getArrayElementType()) ==
1663  DL.getTypeAllocSize(ResElTy)) {
1664  Type *IdxType = DL.getIntPtrType(GEP.getType());
1665  Value *Idx[2] = { Constant::getNullValue(IdxType), GEP.getOperand(1) };
1666  Value *NewGEP =
1667  GEP.isInBounds()
1668  ? Builder->CreateInBoundsGEP(nullptr, StrippedPtr, Idx,
1669  GEP.getName())
1670  : Builder->CreateGEP(nullptr, StrippedPtr, Idx, GEP.getName());
1671 
1672  // V and GEP are both pointer types --> BitCast
1674  GEP.getType());
1675  }
1676 
1677  // Transform things like:
1678  // %V = mul i64 %N, 4
1679  // %t = getelementptr i8* bitcast (i32* %arr to i8*), i32 %V
1680  // into: %t1 = getelementptr i32* %arr, i32 %N; bitcast
1681  if (ResElTy->isSized() && SrcElTy->isSized()) {
1682  // Check that changing the type amounts to dividing the index by a scale
1683  // factor.
1684  uint64_t ResSize = DL.getTypeAllocSize(ResElTy);
1685  uint64_t SrcSize = DL.getTypeAllocSize(SrcElTy);
1686  if (ResSize && SrcSize % ResSize == 0) {
1687  Value *Idx = GEP.getOperand(1);
1688  unsigned BitWidth = Idx->getType()->getPrimitiveSizeInBits();
1689  uint64_t Scale = SrcSize / ResSize;
1690 
1691  // Earlier transforms ensure that the index has type IntPtrType, which
1692  // considerably simplifies the logic by eliminating implicit casts.
1693  assert(Idx->getType() == DL.getIntPtrType(GEP.getType()) &&
1694  "Index not cast to pointer width?");
1695 
1696  bool NSW;
1697  if (Value *NewIdx = Descale(Idx, APInt(BitWidth, Scale), NSW)) {
1698  // Successfully decomposed Idx as NewIdx * Scale, form a new GEP.
1699  // If the multiplication NewIdx * Scale may overflow then the new
1700  // GEP may not be "inbounds".
1701  Value *NewGEP =
1702  GEP.isInBounds() && NSW
1703  ? Builder->CreateInBoundsGEP(nullptr, StrippedPtr, NewIdx,
1704  GEP.getName())
1705  : Builder->CreateGEP(nullptr, StrippedPtr, NewIdx,
1706  GEP.getName());
1707 
1708  // The NewGEP must be pointer typed, so must the old one -> BitCast
1710  GEP.getType());
1711  }
1712  }
1713  }
1714 
1715  // Similarly, transform things like:
1716  // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
1717  // (where tmp = 8*tmp2) into:
1718  // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
1719  if (ResElTy->isSized() && SrcElTy->isSized() && SrcElTy->isArrayTy()) {
1720  // Check that changing to the array element type amounts to dividing the
1721  // index by a scale factor.
1722  uint64_t ResSize = DL.getTypeAllocSize(ResElTy);
1723  uint64_t ArrayEltSize =
1724  DL.getTypeAllocSize(SrcElTy->getArrayElementType());
1725  if (ResSize && ArrayEltSize % ResSize == 0) {
1726  Value *Idx = GEP.getOperand(1);
1727  unsigned BitWidth = Idx->getType()->getPrimitiveSizeInBits();
1728  uint64_t Scale = ArrayEltSize / ResSize;
1729 
1730  // Earlier transforms ensure that the index has type IntPtrType, which
1731  // considerably simplifies the logic by eliminating implicit casts.
1732  assert(Idx->getType() == DL.getIntPtrType(GEP.getType()) &&
1733  "Index not cast to pointer width?");
1734 
1735  bool NSW;
1736  if (Value *NewIdx = Descale(Idx, APInt(BitWidth, Scale), NSW)) {
1737  // Successfully decomposed Idx as NewIdx * Scale, form a new GEP.
1738  // If the multiplication NewIdx * Scale may overflow then the new
1739  // GEP may not be "inbounds".
1740  Value *Off[2] = {
1741  Constant::getNullValue(DL.getIntPtrType(GEP.getType())),
1742  NewIdx};
1743 
1744  Value *NewGEP = GEP.isInBounds() && NSW
1745  ? Builder->CreateInBoundsGEP(
1746  SrcElTy, StrippedPtr, Off, GEP.getName())
1747  : Builder->CreateGEP(SrcElTy, StrippedPtr, Off,
1748  GEP.getName());
1749  // The NewGEP must be pointer typed, so must the old one -> BitCast
1751  GEP.getType());
1752  }
1753  }
1754  }
1755  }
1756  }
1757 
1758  // addrspacecast between types is canonicalized as a bitcast, then an
1759  // addrspacecast. To take advantage of the below bitcast + struct GEP, look
1760  // through the addrspacecast.
1761  if (AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(PtrOp)) {
1762  // X = bitcast A addrspace(1)* to B addrspace(1)*
1763  // Y = addrspacecast A addrspace(1)* to B addrspace(2)*
1764  // Z = gep Y, <...constant indices...>
1765  // Into an addrspacecasted GEP of the struct.
1766  if (BitCastInst *BC = dyn_cast<BitCastInst>(ASC->getOperand(0)))
1767  PtrOp = BC;
1768  }
1769 
1770  /// See if we can simplify:
1771  /// X = bitcast A* to B*
1772  /// Y = gep X, <...constant indices...>
1773  /// into a gep of the original struct. This is important for SROA and alias
1774  /// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged.
1775  if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {
1776  Value *Operand = BCI->getOperand(0);
1777  PointerType *OpType = cast<PointerType>(Operand->getType());
1778  unsigned OffsetBits = DL.getPointerTypeSizeInBits(GEP.getType());
1779  APInt Offset(OffsetBits, 0);
1780  if (!isa<BitCastInst>(Operand) &&
1781  GEP.accumulateConstantOffset(DL, Offset)) {
1782 
1783  // If this GEP instruction doesn't move the pointer, just replace the GEP
1784  // with a bitcast of the real input to the dest type.
1785  if (!Offset) {
1786  // If the bitcast is of an allocation, and the allocation will be
1787  // converted to match the type of the cast, don't touch this.
1788  if (isa<AllocaInst>(Operand) || isAllocationFn(Operand, TLI)) {
1789  // See if the bitcast simplifies, if so, don't nuke this GEP yet.
1790  if (Instruction *I = visitBitCast(*BCI)) {
1791  if (I != BCI) {
1792  I->takeName(BCI);
1793  BCI->getParent()->getInstList().insert(BCI, I);
1794  ReplaceInstUsesWith(*BCI, I);
1795  }
1796  return &GEP;
1797  }
1798  }
1799 
1800  if (Operand->getType()->getPointerAddressSpace() != GEP.getAddressSpace())
1801  return new AddrSpaceCastInst(Operand, GEP.getType());
1802  return new BitCastInst(Operand, GEP.getType());
1803  }
1804 
1805  // Otherwise, if the offset is non-zero, we need to find out if there is a
1806  // field at Offset in 'A's type. If so, we can pull the cast through the
1807  // GEP.
1808  SmallVector<Value*, 8> NewIndices;
1809  if (FindElementAtOffset(OpType, Offset.getSExtValue(), NewIndices)) {
1810  Value *NGEP =
1811  GEP.isInBounds()
1812  ? Builder->CreateInBoundsGEP(nullptr, Operand, NewIndices)
1813  : Builder->CreateGEP(nullptr, Operand, NewIndices);
1814 
1815  if (NGEP->getType() == GEP.getType())
1816  return ReplaceInstUsesWith(GEP, NGEP);
1817  NGEP->takeName(&GEP);
1818 
1819  if (NGEP->getType()->getPointerAddressSpace() != GEP.getAddressSpace())
1820  return new AddrSpaceCastInst(NGEP, GEP.getType());
1821  return new BitCastInst(NGEP, GEP.getType());
1822  }
1823  }
1824  }
1825 
1826  return nullptr;
1827 }
1828 
1829 static bool
1831  const TargetLibraryInfo *TLI) {
1833  Worklist.push_back(AI);
1834 
1835  do {
1836  Instruction *PI = Worklist.pop_back_val();
1837  for (User *U : PI->users()) {
1838  Instruction *I = cast<Instruction>(U);
1839  switch (I->getOpcode()) {
1840  default:
1841  // Give up the moment we see something we can't handle.
1842  return false;
1843 
1844  case Instruction::BitCast:
1845  case Instruction::GetElementPtr:
1846  Users.emplace_back(I);
1847  Worklist.push_back(I);
1848  continue;
1849 
1850  case Instruction::ICmp: {
1851  ICmpInst *ICI = cast<ICmpInst>(I);
1852  // We can fold eq/ne comparisons with null to false/true, respectively.
1853  if (!ICI->isEquality() || !isa<ConstantPointerNull>(ICI->getOperand(1)))
1854  return false;
1855  Users.emplace_back(I);
1856  continue;
1857  }
1858 
1859  case Instruction::Call:
1860  // Ignore no-op and store intrinsics.
1861  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1862  switch (II->getIntrinsicID()) {
1863  default:
1864  return false;
1865 
1866  case Intrinsic::memmove:
1867  case Intrinsic::memcpy:
1868  case Intrinsic::memset: {
1869  MemIntrinsic *MI = cast<MemIntrinsic>(II);
1870  if (MI->isVolatile() || MI->getRawDest() != PI)
1871  return false;
1872  }
1873  // fall through
1874  case Intrinsic::dbg_declare:
1875  case Intrinsic::dbg_value:
1876  case Intrinsic::invariant_start:
1877  case Intrinsic::invariant_end:
1878  case Intrinsic::lifetime_start:
1879  case Intrinsic::lifetime_end:
1880  case Intrinsic::objectsize:
1881  Users.emplace_back(I);
1882  continue;
1883  }
1884  }
1885 
1886  if (isFreeCall(I, TLI)) {
1887  Users.emplace_back(I);
1888  continue;
1889  }
1890  return false;
1891 
1892  case Instruction::Store: {
1893  StoreInst *SI = cast<StoreInst>(I);
1894  if (SI->isVolatile() || SI->getPointerOperand() != PI)
1895  return false;
1896  Users.emplace_back(I);
1897  continue;
1898  }
1899  }
1900  llvm_unreachable("missing a return?");
1901  }
1902  } while (!Worklist.empty());
1903  return true;
1904 }
1905 
1907  // If we have a malloc call which is only used in any amount of comparisons
1908  // to null and free calls, delete the calls and replace the comparisons with
1909  // true or false as appropriate.
1911  if (isAllocSiteRemovable(&MI, Users, TLI)) {
1912  for (unsigned i = 0, e = Users.size(); i != e; ++i) {
1913  Instruction *I = cast_or_null<Instruction>(&*Users[i]);
1914  if (!I) continue;
1915 
1916  if (ICmpInst *C = dyn_cast<ICmpInst>(I)) {
1917  ReplaceInstUsesWith(*C,
1919  C->isFalseWhenEqual()));
1920  } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I)) {
1921  ReplaceInstUsesWith(*I, UndefValue::get(I->getType()));
1922  } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1923  if (II->getIntrinsicID() == Intrinsic::objectsize) {
1924  ConstantInt *CI = cast<ConstantInt>(II->getArgOperand(1));
1925  uint64_t DontKnow = CI->isZero() ? -1ULL : 0;
1926  ReplaceInstUsesWith(*I, ConstantInt::get(I->getType(), DontKnow));
1927  }
1928  }
1929  EraseInstFromFunction(*I);
1930  }
1931 
1932  if (InvokeInst *II = dyn_cast<InvokeInst>(&MI)) {
1933  // Replace invoke with a NOP intrinsic to maintain the original CFG
1934  Module *M = II->getParent()->getParent()->getParent();
1935  Function *F = Intrinsic::getDeclaration(M, Intrinsic::donothing);
1936  InvokeInst::Create(F, II->getNormalDest(), II->getUnwindDest(),
1937  None, "", II->getParent());
1938  }
1939  return EraseInstFromFunction(MI);
1940  }
1941  return nullptr;
1942 }
1943 
1944 /// \brief Move the call to free before a NULL test.
1945 ///
1946 /// Check if this free is accessed after its argument has been test
1947 /// against NULL (property 0).
1948 /// If yes, it is legal to move this call in its predecessor block.
1949 ///
1950 /// The move is performed only if the block containing the call to free
1951 /// will be removed, i.e.:
1952 /// 1. it has only one predecessor P, and P has two successors
1953 /// 2. it contains the call and an unconditional branch
1954 /// 3. its successor is the same as its predecessor's successor
1955 ///
1956 /// The profitability is out-of concern here and this function should
1957 /// be called only if the caller knows this transformation would be
1958 /// profitable (e.g., for code size).
1959 static Instruction *
1961  Value *Op = FI.getArgOperand(0);
1962  BasicBlock *FreeInstrBB = FI.getParent();
1963  BasicBlock *PredBB = FreeInstrBB->getSinglePredecessor();
1964 
1965  // Validate part of constraint #1: Only one predecessor
1966  // FIXME: We can extend the number of predecessor, but in that case, we
1967  // would duplicate the call to free in each predecessor and it may
1968  // not be profitable even for code size.
1969  if (!PredBB)
1970  return nullptr;
1971 
1972  // Validate constraint #2: Does this block contains only the call to
1973  // free and an unconditional branch?
1974  // FIXME: We could check if we can speculate everything in the
1975  // predecessor block
1976  if (FreeInstrBB->size() != 2)
1977  return nullptr;
1978  BasicBlock *SuccBB;
1979  if (!match(FreeInstrBB->getTerminator(), m_UnconditionalBr(SuccBB)))
1980  return nullptr;
1981 
1982  // Validate the rest of constraint #1 by matching on the pred branch.
1983  TerminatorInst *TI = PredBB->getTerminator();
1984  BasicBlock *TrueBB, *FalseBB;
1985  ICmpInst::Predicate Pred;
1986  if (!match(TI, m_Br(m_ICmp(Pred, m_Specific(Op), m_Zero()), TrueBB, FalseBB)))
1987  return nullptr;
1988  if (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
1989  return nullptr;
1990 
1991  // Validate constraint #3: Ensure the null case just falls through.
1992  if (SuccBB != (Pred == ICmpInst::ICMP_EQ ? TrueBB : FalseBB))
1993  return nullptr;
1994  assert(FreeInstrBB == (Pred == ICmpInst::ICMP_EQ ? FalseBB : TrueBB) &&
1995  "Broken CFG: missing edge from predecessor to successor");
1996 
1997  FI.moveBefore(TI);
1998  return &FI;
1999 }
2000 
2001 
2003  Value *Op = FI.getArgOperand(0);
2004 
2005  // free undef -> unreachable.
2006  if (isa<UndefValue>(Op)) {
2007  // Insert a new store to null because we cannot modify the CFG here.
2008  Builder->CreateStore(ConstantInt::getTrue(FI.getContext()),
2010  return EraseInstFromFunction(FI);
2011  }
2012 
2013  // If we have 'free null' delete the instruction. This can happen in stl code
2014  // when lots of inlining happens.
2015  if (isa<ConstantPointerNull>(Op))
2016  return EraseInstFromFunction(FI);
2017 
2018  // If we optimize for code size, try to move the call to free before the null
2019  // test so that simplify cfg can remove the empty block and dead code
2020  // elimination the branch. I.e., helps to turn something like:
2021  // if (foo) free(foo);
2022  // into
2023  // free(foo);
2024  if (MinimizeSize)
2026  return I;
2027 
2028  return nullptr;
2029 }
2030 
2032  if (RI.getNumOperands() == 0) // ret void
2033  return nullptr;
2034 
2035  Value *ResultOp = RI.getOperand(0);
2036  Type *VTy = ResultOp->getType();
2037  if (!VTy->isIntegerTy())
2038  return nullptr;
2039 
2040  // There might be assume intrinsics dominating this return that completely
2041  // determine the value. If so, constant fold it.
2042  unsigned BitWidth = VTy->getPrimitiveSizeInBits();
2043  APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2044  computeKnownBits(ResultOp, KnownZero, KnownOne, 0, &RI);
2045  if ((KnownZero|KnownOne).isAllOnesValue())
2046  RI.setOperand(0, Constant::getIntegerValue(VTy, KnownOne));
2047 
2048  return nullptr;
2049 }
2050 
2052  // Change br (not X), label True, label False to: br X, label False, True
2053  Value *X = nullptr;
2054  BasicBlock *TrueDest;
2055  BasicBlock *FalseDest;
2056  if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
2057  !isa<Constant>(X)) {
2058  // Swap Destinations and condition...
2059  BI.setCondition(X);
2060  BI.swapSuccessors();
2061  return &BI;
2062  }
2063 
2064  // If the condition is irrelevant, remove the use so that other
2065  // transforms on the condition become more effective.
2066  if (BI.isConditional() &&
2067  BI.getSuccessor(0) == BI.getSuccessor(1) &&
2068  !isa<UndefValue>(BI.getCondition())) {
2070  return &BI;
2071  }
2072 
2073  // Canonicalize fcmp_one -> fcmp_oeq
2074  FCmpInst::Predicate FPred; Value *Y;
2075  if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)),
2076  TrueDest, FalseDest)) &&
2077  BI.getCondition()->hasOneUse())
2078  if (FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
2079  FPred == FCmpInst::FCMP_OGE) {
2080  FCmpInst *Cond = cast<FCmpInst>(BI.getCondition());
2082 
2083  // Swap Destinations and condition.
2084  BI.swapSuccessors();
2085  Worklist.Add(Cond);
2086  return &BI;
2087  }
2088 
2089  // Canonicalize icmp_ne -> icmp_eq
2090  ICmpInst::Predicate IPred;
2091  if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
2092  TrueDest, FalseDest)) &&
2093  BI.getCondition()->hasOneUse())
2094  if (IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE ||
2095  IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
2096  IPred == ICmpInst::ICMP_SGE) {
2097  ICmpInst *Cond = cast<ICmpInst>(BI.getCondition());
2099  // Swap Destinations and condition.
2100  BI.swapSuccessors();
2101  Worklist.Add(Cond);
2102  return &BI;
2103  }
2104 
2105  return nullptr;
2106 }
2107 
2109  Value *Cond = SI.getCondition();
2110  unsigned BitWidth = cast<IntegerType>(Cond->getType())->getBitWidth();
2111  APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2112  computeKnownBits(Cond, KnownZero, KnownOne, 0, &SI);
2113  unsigned LeadingKnownZeros = KnownZero.countLeadingOnes();
2114  unsigned LeadingKnownOnes = KnownOne.countLeadingOnes();
2115 
2116  // Compute the number of leading bits we can ignore.
2117  for (auto &C : SI.cases()) {
2118  LeadingKnownZeros = std::min(
2119  LeadingKnownZeros, C.getCaseValue()->getValue().countLeadingZeros());
2120  LeadingKnownOnes = std::min(
2121  LeadingKnownOnes, C.getCaseValue()->getValue().countLeadingOnes());
2122  }
2123 
2124  unsigned NewWidth = BitWidth - std::max(LeadingKnownZeros, LeadingKnownOnes);
2125 
2126  // Truncate the condition operand if the new type is equal to or larger than
2127  // the largest legal integer type. We need to be conservative here since
2128  // x86 generates redundant zero-extension instructions if the operand is
2129  // truncated to i8 or i16.
2130  bool TruncCond = false;
2131  if (NewWidth > 0 && BitWidth > NewWidth &&
2132  NewWidth >= DL.getLargestLegalIntTypeSize()) {
2133  TruncCond = true;
2134  IntegerType *Ty = IntegerType::get(SI.getContext(), NewWidth);
2135  Builder->SetInsertPoint(&SI);
2136  Value *NewCond = Builder->CreateTrunc(SI.getCondition(), Ty, "trunc");
2137  SI.setCondition(NewCond);
2138 
2139  for (auto &C : SI.cases())
2140  static_cast<SwitchInst::CaseIt *>(&C)->setValue(ConstantInt::get(
2141  SI.getContext(), C.getCaseValue()->getValue().trunc(NewWidth)));
2142  }
2143 
2144  if (Instruction *I = dyn_cast<Instruction>(Cond)) {
2145  if (I->getOpcode() == Instruction::Add)
2146  if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
2147  // change 'switch (X+4) case 1:' into 'switch (X) case -3'
2148  // Skip the first item since that's the default case.
2149  for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end();
2150  i != e; ++i) {
2151  ConstantInt* CaseVal = i.getCaseValue();
2152  Constant *LHS = CaseVal;
2153  if (TruncCond)
2154  LHS = LeadingKnownZeros
2155  ? ConstantExpr::getZExt(CaseVal, Cond->getType())
2156  : ConstantExpr::getSExt(CaseVal, Cond->getType());
2157  Constant* NewCaseVal = ConstantExpr::getSub(LHS, AddRHS);
2158  assert(isa<ConstantInt>(NewCaseVal) &&
2159  "Result of expression should be constant");
2160  i.setValue(cast<ConstantInt>(NewCaseVal));
2161  }
2162  SI.setCondition(I->getOperand(0));
2163  Worklist.Add(I);
2164  return &SI;
2165  }
2166  }
2167 
2168  return TruncCond ? &SI : nullptr;
2169 }
2170 
2172  Value *Agg = EV.getAggregateOperand();
2173 
2174  if (!EV.hasIndices())
2175  return ReplaceInstUsesWith(EV, Agg);
2176 
2177  if (Value *V =
2178  SimplifyExtractValueInst(Agg, EV.getIndices(), DL, TLI, DT, AC))
2179  return ReplaceInstUsesWith(EV, V);
2180 
2181  if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
2182  // We're extracting from an insertvalue instruction, compare the indices
2183  const unsigned *exti, *exte, *insi, *inse;
2184  for (exti = EV.idx_begin(), insi = IV->idx_begin(),
2185  exte = EV.idx_end(), inse = IV->idx_end();
2186  exti != exte && insi != inse;
2187  ++exti, ++insi) {
2188  if (*insi != *exti)
2189  // The insert and extract both reference distinctly different elements.
2190  // This means the extract is not influenced by the insert, and we can
2191  // replace the aggregate operand of the extract with the aggregate
2192  // operand of the insert. i.e., replace
2193  // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
2194  // %E = extractvalue { i32, { i32 } } %I, 0
2195  // with
2196  // %E = extractvalue { i32, { i32 } } %A, 0
2197  return ExtractValueInst::Create(IV->getAggregateOperand(),
2198  EV.getIndices());
2199  }
2200  if (exti == exte && insi == inse)
2201  // Both iterators are at the end: Index lists are identical. Replace
2202  // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
2203  // %C = extractvalue { i32, { i32 } } %B, 1, 0
2204  // with "i32 42"
2205  return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand());
2206  if (exti == exte) {
2207  // The extract list is a prefix of the insert list. i.e. replace
2208  // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
2209  // %E = extractvalue { i32, { i32 } } %I, 1
2210  // with
2211  // %X = extractvalue { i32, { i32 } } %A, 1
2212  // %E = insertvalue { i32 } %X, i32 42, 0
2213  // by switching the order of the insert and extract (though the
2214  // insertvalue should be left in, since it may have other uses).
2215  Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(),
2216  EV.getIndices());
2217  return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
2218  makeArrayRef(insi, inse));
2219  }
2220  if (insi == inse)
2221  // The insert list is a prefix of the extract list
2222  // We can simply remove the common indices from the extract and make it
2223  // operate on the inserted value instead of the insertvalue result.
2224  // i.e., replace
2225  // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
2226  // %E = extractvalue { i32, { i32 } } %I, 1, 0
2227  // with
2228  // %E extractvalue { i32 } { i32 42 }, 0
2229  return ExtractValueInst::Create(IV->getInsertedValueOperand(),
2230  makeArrayRef(exti, exte));
2231  }
2232  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Agg)) {
2233  // We're extracting from an intrinsic, see if we're the only user, which
2234  // allows us to simplify multiple result intrinsics to simpler things that
2235  // just get one value.
2236  if (II->hasOneUse()) {
2237  // Check if we're grabbing the overflow bit or the result of a 'with
2238  // overflow' intrinsic. If it's the latter we can remove the intrinsic
2239  // and replace it with a traditional binary instruction.
2240  switch (II->getIntrinsicID()) {
2241  case Intrinsic::uadd_with_overflow:
2242  case Intrinsic::sadd_with_overflow:
2243  if (*EV.idx_begin() == 0) { // Normal result.
2244  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
2245  ReplaceInstUsesWith(*II, UndefValue::get(II->getType()));
2246  EraseInstFromFunction(*II);
2247  return BinaryOperator::CreateAdd(LHS, RHS);
2248  }
2249 
2250  // If the normal result of the add is dead, and the RHS is a constant,
2251  // we can transform this into a range comparison.
2252  // overflow = uadd a, -4 --> overflow = icmp ugt a, 3
2253  if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow)
2254  if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getArgOperand(1)))
2255  return new ICmpInst(ICmpInst::ICMP_UGT, II->getArgOperand(0),
2256  ConstantExpr::getNot(CI));
2257  break;
2258  case Intrinsic::usub_with_overflow:
2259  case Intrinsic::ssub_with_overflow:
2260  if (*EV.idx_begin() == 0) { // Normal result.
2261  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
2262  ReplaceInstUsesWith(*II, UndefValue::get(II->getType()));
2263  EraseInstFromFunction(*II);
2264  return BinaryOperator::CreateSub(LHS, RHS);
2265  }
2266  break;
2267  case Intrinsic::umul_with_overflow:
2268  case Intrinsic::smul_with_overflow:
2269  if (*EV.idx_begin() == 0) { // Normal result.
2270  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
2271  ReplaceInstUsesWith(*II, UndefValue::get(II->getType()));
2272  EraseInstFromFunction(*II);
2273  return BinaryOperator::CreateMul(LHS, RHS);
2274  }
2275  break;
2276  default:
2277  break;
2278  }
2279  }
2280  }
2281  if (LoadInst *L = dyn_cast<LoadInst>(Agg))
2282  // If the (non-volatile) load only has one use, we can rewrite this to a
2283  // load from a GEP. This reduces the size of the load.
2284  // FIXME: If a load is used only by extractvalue instructions then this
2285  // could be done regardless of having multiple uses.
2286  if (L->isSimple() && L->hasOneUse()) {
2287  // extractvalue has integer indices, getelementptr has Value*s. Convert.
2288  SmallVector<Value*, 4> Indices;
2289  // Prefix an i32 0 since we need the first element.
2290  Indices.push_back(Builder->getInt32(0));
2291  for (ExtractValueInst::idx_iterator I = EV.idx_begin(), E = EV.idx_end();
2292  I != E; ++I)
2293  Indices.push_back(Builder->getInt32(*I));
2294 
2295  // We need to insert these at the location of the old load, not at that of
2296  // the extractvalue.
2297  Builder->SetInsertPoint(L->getParent(), L);
2298  Value *GEP = Builder->CreateInBoundsGEP(L->getType(),
2299  L->getPointerOperand(), Indices);
2300  // Returning the load directly will cause the main loop to insert it in
2301  // the wrong spot, so use ReplaceInstUsesWith().
2302  return ReplaceInstUsesWith(EV, Builder->CreateLoad(GEP));
2303  }
2304  // We could simplify extracts from other values. Note that nested extracts may
2305  // already be simplified implicitly by the above: extract (extract (insert) )
2306  // will be translated into extract ( insert ( extract ) ) first and then just
2307  // the value inserted, if appropriate. Similarly for extracts from single-use
2308  // loads: extract (extract (load)) will be translated to extract (load (gep))
2309  // and if again single-use then via load (gep (gep)) to load (gep).
2310  // However, double extracts from e.g. function arguments or return values
2311  // aren't handled yet.
2312  return nullptr;
2313 }
2314 
2315 /// isCatchAll - Return 'true' if the given typeinfo will match anything.
2316 static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo) {
2317  switch (Personality) {
2318  case EHPersonality::GNU_C:
2319  // The GCC C EH personality only exists to support cleanups, so it's not
2320  // clear what the semantics of catch clauses are.
2321  return false;
2323  return false;
2325  // While __gnat_all_others_value will match any Ada exception, it doesn't
2326  // match foreign exceptions (or didn't, before gcc-4.7).
2327  return false;
2333  return TypeInfo->isNullValue();
2334  }
2335  llvm_unreachable("invalid enum");
2336 }
2337 
2338 static bool shorter_filter(const Value *LHS, const Value *RHS) {
2339  return
2340  cast<ArrayType>(LHS->getType())->getNumElements()
2341  <
2342  cast<ArrayType>(RHS->getType())->getNumElements();
2343 }
2344 
2346  // The logic here should be correct for any real-world personality function.
2347  // However if that turns out not to be true, the offending logic can always
2348  // be conditioned on the personality function, like the catch-all logic is.
2349  EHPersonality Personality =
2351 
2352  // Simplify the list of clauses, eg by removing repeated catch clauses
2353  // (these are often created by inlining).
2354  bool MakeNewInstruction = false; // If true, recreate using the following:
2355  SmallVector<Constant *, 16> NewClauses; // - Clauses for the new instruction;
2356  bool CleanupFlag = LI.isCleanup(); // - The new instruction is a cleanup.
2357 
2358  SmallPtrSet<Value *, 16> AlreadyCaught; // Typeinfos known caught already.
2359  for (unsigned i = 0, e = LI.getNumClauses(); i != e; ++i) {
2360  bool isLastClause = i + 1 == e;
2361  if (LI.isCatch(i)) {
2362  // A catch clause.
2363  Constant *CatchClause = LI.getClause(i);
2364  Constant *TypeInfo = CatchClause->stripPointerCasts();
2365 
2366  // If we already saw this clause, there is no point in having a second
2367  // copy of it.
2368  if (AlreadyCaught.insert(TypeInfo).second) {
2369  // This catch clause was not already seen.
2370  NewClauses.push_back(CatchClause);
2371  } else {
2372  // Repeated catch clause - drop the redundant copy.
2373  MakeNewInstruction = true;
2374  }
2375 
2376  // If this is a catch-all then there is no point in keeping any following
2377  // clauses or marking the landingpad as having a cleanup.
2378  if (isCatchAll(Personality, TypeInfo)) {
2379  if (!isLastClause)
2380  MakeNewInstruction = true;
2381  CleanupFlag = false;
2382  break;
2383  }
2384  } else {
2385  // A filter clause. If any of the filter elements were already caught
2386  // then they can be dropped from the filter. It is tempting to try to
2387  // exploit the filter further by saying that any typeinfo that does not
2388  // occur in the filter can't be caught later (and thus can be dropped).
2389  // However this would be wrong, since typeinfos can match without being
2390  // equal (for example if one represents a C++ class, and the other some
2391  // class derived from it).
2392  assert(LI.isFilter(i) && "Unsupported landingpad clause!");
2393  Constant *FilterClause = LI.getClause(i);
2394  ArrayType *FilterType = cast<ArrayType>(FilterClause->getType());
2395  unsigned NumTypeInfos = FilterType->getNumElements();
2396 
2397  // An empty filter catches everything, so there is no point in keeping any
2398  // following clauses or marking the landingpad as having a cleanup. By
2399  // dealing with this case here the following code is made a bit simpler.
2400  if (!NumTypeInfos) {
2401  NewClauses.push_back(FilterClause);
2402  if (!isLastClause)
2403  MakeNewInstruction = true;
2404  CleanupFlag = false;
2405  break;
2406  }
2407 
2408  bool MakeNewFilter = false; // If true, make a new filter.
2409  SmallVector<Constant *, 16> NewFilterElts; // New elements.
2410  if (isa<ConstantAggregateZero>(FilterClause)) {
2411  // Not an empty filter - it contains at least one null typeinfo.
2412  assert(NumTypeInfos > 0 && "Should have handled empty filter already!");
2413  Constant *TypeInfo =
2414  Constant::getNullValue(FilterType->getElementType());
2415  // If this typeinfo is a catch-all then the filter can never match.
2416  if (isCatchAll(Personality, TypeInfo)) {
2417  // Throw the filter away.
2418  MakeNewInstruction = true;
2419  continue;
2420  }
2421 
2422  // There is no point in having multiple copies of this typeinfo, so
2423  // discard all but the first copy if there is more than one.
2424  NewFilterElts.push_back(TypeInfo);
2425  if (NumTypeInfos > 1)
2426  MakeNewFilter = true;
2427  } else {
2428  ConstantArray *Filter = cast<ConstantArray>(FilterClause);
2429  SmallPtrSet<Value *, 16> SeenInFilter; // For uniquing the elements.
2430  NewFilterElts.reserve(NumTypeInfos);
2431 
2432  // Remove any filter elements that were already caught or that already
2433  // occurred in the filter. While there, see if any of the elements are
2434  // catch-alls. If so, the filter can be discarded.
2435  bool SawCatchAll = false;
2436  for (unsigned j = 0; j != NumTypeInfos; ++j) {
2437  Constant *Elt = Filter->getOperand(j);
2438  Constant *TypeInfo = Elt->stripPointerCasts();
2439  if (isCatchAll(Personality, TypeInfo)) {
2440  // This element is a catch-all. Bail out, noting this fact.
2441  SawCatchAll = true;
2442  break;
2443  }
2444  if (AlreadyCaught.count(TypeInfo))
2445  // Already caught by an earlier clause, so having it in the filter
2446  // is pointless.
2447  continue;
2448  // There is no point in having multiple copies of the same typeinfo in
2449  // a filter, so only add it if we didn't already.
2450  if (SeenInFilter.insert(TypeInfo).second)
2451  NewFilterElts.push_back(cast<Constant>(Elt));
2452  }
2453  // A filter containing a catch-all cannot match anything by definition.
2454  if (SawCatchAll) {
2455  // Throw the filter away.
2456  MakeNewInstruction = true;
2457  continue;
2458  }
2459 
2460  // If we dropped something from the filter, make a new one.
2461  if (NewFilterElts.size() < NumTypeInfos)
2462  MakeNewFilter = true;
2463  }
2464  if (MakeNewFilter) {
2465  FilterType = ArrayType::get(FilterType->getElementType(),
2466  NewFilterElts.size());
2467  FilterClause = ConstantArray::get(FilterType, NewFilterElts);
2468  MakeNewInstruction = true;
2469  }
2470 
2471  NewClauses.push_back(FilterClause);
2472 
2473  // If the new filter is empty then it will catch everything so there is
2474  // no point in keeping any following clauses or marking the landingpad
2475  // as having a cleanup. The case of the original filter being empty was
2476  // already handled above.
2477  if (MakeNewFilter && !NewFilterElts.size()) {
2478  assert(MakeNewInstruction && "New filter but not a new instruction!");
2479  CleanupFlag = false;
2480  break;
2481  }
2482  }
2483  }
2484 
2485  // If several filters occur in a row then reorder them so that the shortest
2486  // filters come first (those with the smallest number of elements). This is
2487  // advantageous because shorter filters are more likely to match, speeding up
2488  // unwinding, but mostly because it increases the effectiveness of the other
2489  // filter optimizations below.
2490  for (unsigned i = 0, e = NewClauses.size(); i + 1 < e; ) {
2491  unsigned j;
2492  // Find the maximal 'j' s.t. the range [i, j) consists entirely of filters.
2493  for (j = i; j != e; ++j)
2494  if (!isa<ArrayType>(NewClauses[j]->getType()))
2495  break;
2496 
2497  // Check whether the filters are already sorted by length. We need to know
2498  // if sorting them is actually going to do anything so that we only make a
2499  // new landingpad instruction if it does.
2500  for (unsigned k = i; k + 1 < j; ++k)
2501  if (shorter_filter(NewClauses[k+1], NewClauses[k])) {
2502  // Not sorted, so sort the filters now. Doing an unstable sort would be
2503  // correct too but reordering filters pointlessly might confuse users.
2504  std::stable_sort(NewClauses.begin() + i, NewClauses.begin() + j,
2505  shorter_filter);
2506  MakeNewInstruction = true;
2507  break;
2508  }
2509 
2510  // Look for the next batch of filters.
2511  i = j + 1;
2512  }
2513 
2514  // If typeinfos matched if and only if equal, then the elements of a filter L
2515  // that occurs later than a filter F could be replaced by the intersection of
2516  // the elements of F and L. In reality two typeinfos can match without being
2517  // equal (for example if one represents a C++ class, and the other some class
2518  // derived from it) so it would be wrong to perform this transform in general.
2519  // However the transform is correct and useful if F is a subset of L. In that
2520  // case L can be replaced by F, and thus removed altogether since repeating a
2521  // filter is pointless. So here we look at all pairs of filters F and L where
2522  // L follows F in the list of clauses, and remove L if every element of F is
2523  // an element of L. This can occur when inlining C++ functions with exception
2524  // specifications.
2525  for (unsigned i = 0; i + 1 < NewClauses.size(); ++i) {
2526  // Examine each filter in turn.
2527  Value *Filter = NewClauses[i];
2528  ArrayType *FTy = dyn_cast<ArrayType>(Filter->getType());
2529  if (!FTy)
2530  // Not a filter - skip it.
2531  continue;
2532  unsigned FElts = FTy->getNumElements();
2533  // Examine each filter following this one. Doing this backwards means that
2534  // we don't have to worry about filters disappearing under us when removed.
2535  for (unsigned j = NewClauses.size() - 1; j != i; --j) {
2536  Value *LFilter = NewClauses[j];
2537  ArrayType *LTy = dyn_cast<ArrayType>(LFilter->getType());
2538  if (!LTy)
2539  // Not a filter - skip it.
2540  continue;
2541  // If Filter is a subset of LFilter, i.e. every element of Filter is also
2542  // an element of LFilter, then discard LFilter.
2543  SmallVectorImpl<Constant *>::iterator J = NewClauses.begin() + j;
2544  // If Filter is empty then it is a subset of LFilter.
2545  if (!FElts) {
2546  // Discard LFilter.
2547  NewClauses.erase(J);
2548  MakeNewInstruction = true;
2549  // Move on to the next filter.
2550  continue;
2551  }
2552  unsigned LElts = LTy->getNumElements();
2553  // If Filter is longer than LFilter then it cannot be a subset of it.
2554  if (FElts > LElts)
2555  // Move on to the next filter.
2556  continue;
2557  // At this point we know that LFilter has at least one element.
2558  if (isa<ConstantAggregateZero>(LFilter)) { // LFilter only contains zeros.
2559  // Filter is a subset of LFilter iff Filter contains only zeros (as we
2560  // already know that Filter is not longer than LFilter).
2561  if (isa<ConstantAggregateZero>(Filter)) {
2562  assert(FElts <= LElts && "Should have handled this case earlier!");
2563  // Discard LFilter.
2564  NewClauses.erase(J);
2565  MakeNewInstruction = true;
2566  }
2567  // Move on to the next filter.
2568  continue;
2569  }
2570  ConstantArray *LArray = cast<ConstantArray>(LFilter);
2571  if (isa<ConstantAggregateZero>(Filter)) { // Filter only contains zeros.
2572  // Since Filter is non-empty and contains only zeros, it is a subset of
2573  // LFilter iff LFilter contains a zero.
2574  assert(FElts > 0 && "Should have eliminated the empty filter earlier!");
2575  for (unsigned l = 0; l != LElts; ++l)
2576  if (LArray->getOperand(l)->isNullValue()) {
2577  // LFilter contains a zero - discard it.
2578  NewClauses.erase(J);
2579  MakeNewInstruction = true;
2580  break;
2581  }
2582  // Move on to the next filter.
2583  continue;
2584  }
2585  // At this point we know that both filters are ConstantArrays. Loop over
2586  // operands to see whether every element of Filter is also an element of
2587  // LFilter. Since filters tend to be short this is probably faster than
2588  // using a method that scales nicely.
2589  ConstantArray *FArray = cast<ConstantArray>(Filter);
2590  bool AllFound = true;
2591  for (unsigned f = 0; f != FElts; ++f) {
2592  Value *FTypeInfo = FArray->getOperand(f)->stripPointerCasts();
2593  AllFound = false;
2594  for (unsigned l = 0; l != LElts; ++l) {
2595  Value *LTypeInfo = LArray->getOperand(l)->stripPointerCasts();
2596  if (LTypeInfo == FTypeInfo) {
2597  AllFound = true;
2598  break;
2599  }
2600  }
2601  if (!AllFound)
2602  break;
2603  }
2604  if (AllFound) {
2605  // Discard LFilter.
2606  NewClauses.erase(J);
2607  MakeNewInstruction = true;
2608  }
2609  // Move on to the next filter.
2610  }
2611  }
2612 
2613  // If we changed any of the clauses, replace the old landingpad instruction
2614  // with a new one.
2615  if (MakeNewInstruction) {
2617  NewClauses.size());
2618  for (unsigned i = 0, e = NewClauses.size(); i != e; ++i)
2619  NLI->addClause(NewClauses[i]);
2620  // A landing pad with no clauses must have the cleanup flag set. It is
2621  // theoretically possible, though highly unlikely, that we eliminated all
2622  // clauses. If so, force the cleanup flag to true.
2623  if (NewClauses.empty())
2624  CleanupFlag = true;
2625  NLI->setCleanup(CleanupFlag);
2626  return NLI;
2627  }
2628 
2629  // Even if none of the clauses changed, we may nonetheless have understood
2630  // that the cleanup flag is pointless. Clear it if so.
2631  if (LI.isCleanup() != CleanupFlag) {
2632  assert(!CleanupFlag && "Adding a cleanup, not removing one?!");
2633  LI.setCleanup(CleanupFlag);
2634  return &LI;
2635  }
2636 
2637  return nullptr;
2638 }
2639 
2640 /// TryToSinkInstruction - Try to move the specified instruction from its
2641 /// current block into the beginning of DestBlock, which can only happen if it's
2642 /// safe to move the instruction past all of the instructions between it and the
2643 /// end of its block.
2644 static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
2645  assert(I->hasOneUse() && "Invariants didn't hold!");
2646 
2647  // Cannot move control-flow-involving, volatile loads, vaarg, etc.
2648  if (isa<PHINode>(I) || isa<LandingPadInst>(I) || I->mayHaveSideEffects() ||
2649  isa<TerminatorInst>(I))
2650  return false;
2651 
2652  // Do not sink alloca instructions out of the entry block.
2653  if (isa<AllocaInst>(I) && I->getParent() ==
2654  &DestBlock->getParent()->getEntryBlock())
2655  return false;
2656 
2657  // We can only sink load instructions if there is nothing between the load and
2658  // the end of block that could change the value.
2659  if (I->mayReadFromMemory()) {
2660  for (BasicBlock::iterator Scan = I, E = I->getParent()->end();
2661  Scan != E; ++Scan)
2662  if (Scan->mayWriteToMemory())
2663  return false;
2664  }
2665 
2666  BasicBlock::iterator InsertPos = DestBlock->getFirstInsertionPt();
2667  I->moveBefore(InsertPos);
2668  ++NumSunkInst;
2669  return true;
2670 }
2671 
2673  while (!Worklist.isEmpty()) {
2674  Instruction *I = Worklist.RemoveOne();
2675  if (I == nullptr) continue; // skip null values.
2676 
2677  // Check to see if we can DCE the instruction.
2678  if (isInstructionTriviallyDead(I, TLI)) {
2679  DEBUG(dbgs() << "IC: DCE: " << *I << '\n');
2680  EraseInstFromFunction(*I);
2681  ++NumDeadInst;
2682  MadeIRChange = true;
2683  continue;
2684  }
2685 
2686  // Instruction isn't dead, see if we can constant propagate it.
2687  if (!I->use_empty() &&
2688  (I->getNumOperands() == 0 || isa<Constant>(I->getOperand(0)))) {
2689  if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
2690  DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n');
2691 
2692  // Add operands to the worklist.
2693  ReplaceInstUsesWith(*I, C);
2694  ++NumConstProp;
2695  EraseInstFromFunction(*I);
2696  MadeIRChange = true;
2697  continue;
2698  }
2699  }
2700 
2701  // See if we can trivially sink this instruction to a successor basic block.
2702  if (I->hasOneUse()) {
2703  BasicBlock *BB = I->getParent();
2704  Instruction *UserInst = cast<Instruction>(*I->user_begin());
2705  BasicBlock *UserParent;
2706 
2707  // Get the block the use occurs in.
2708  if (PHINode *PN = dyn_cast<PHINode>(UserInst))
2709  UserParent = PN->getIncomingBlock(*I->use_begin());
2710  else
2711  UserParent = UserInst->getParent();
2712 
2713  if (UserParent != BB) {
2714  bool UserIsSuccessor = false;
2715  // See if the user is one of our successors.
2716  for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
2717  if (*SI == UserParent) {
2718  UserIsSuccessor = true;
2719  break;
2720  }
2721 
2722  // If the user is one of our immediate successors, and if that successor
2723  // only has us as a predecessors (we'd have to split the critical edge
2724  // otherwise), we can keep going.
2725  if (UserIsSuccessor && UserParent->getSinglePredecessor()) {
2726  // Okay, the CFG is simple enough, try to sink this instruction.
2727  if (TryToSinkInstruction(I, UserParent)) {
2728  MadeIRChange = true;
2729  // We'll add uses of the sunk instruction below, but since sinking
2730  // can expose opportunities for it's *operands* add them to the
2731  // worklist
2732  for (Use &U : I->operands())
2733  if (Instruction *OpI = dyn_cast<Instruction>(U.get()))
2734  Worklist.Add(OpI);
2735  }
2736  }
2737  }
2738  }
2739 
2740  // Now that we have an instruction, try combining it to simplify it.
2741  Builder->SetInsertPoint(I->getParent(), I);
2742  Builder->SetCurrentDebugLocation(I->getDebugLoc());
2743 
2744 #ifndef NDEBUG
2745  std::string OrigI;
2746 #endif
2747  DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str(););
2748  DEBUG(dbgs() << "IC: Visiting: " << OrigI << '\n');
2749 
2750  if (Instruction *Result = visit(*I)) {
2751  ++NumCombined;
2752  // Should we replace the old instruction with a new one?
2753  if (Result != I) {
2754  DEBUG(dbgs() << "IC: Old = " << *I << '\n'
2755  << " New = " << *Result << '\n');
2756 
2757  if (I->getDebugLoc())
2758  Result->setDebugLoc(I->getDebugLoc());
2759  // Everything uses the new instruction now.
2760  I->replaceAllUsesWith(Result);
2761 
2762  // Move the name to the new instruction first.
2763  Result->takeName(I);
2764 
2765  // Push the new instruction and any users onto the worklist.
2766  Worklist.Add(Result);
2767  Worklist.AddUsersToWorkList(*Result);
2768 
2769  // Insert the new instruction into the basic block...
2770  BasicBlock *InstParent = I->getParent();
2771  BasicBlock::iterator InsertPos = I;
2772 
2773  // If we replace a PHI with something that isn't a PHI, fix up the
2774  // insertion point.
2775  if (!isa<PHINode>(Result) && isa<PHINode>(InsertPos))
2776  InsertPos = InstParent->getFirstInsertionPt();
2777 
2778  InstParent->getInstList().insert(InsertPos, Result);
2779 
2780  EraseInstFromFunction(*I);
2781  } else {
2782 #ifndef NDEBUG
2783  DEBUG(dbgs() << "IC: Mod = " << OrigI << '\n'
2784  << " New = " << *I << '\n');
2785 #endif
2786 
2787  // If the instruction was modified, it's possible that it is now dead.
2788  // if so, remove it.
2789  if (isInstructionTriviallyDead(I, TLI)) {
2790  EraseInstFromFunction(*I);
2791  } else {
2792  Worklist.Add(I);
2793  Worklist.AddUsersToWorkList(*I);
2794  }
2795  }
2796  MadeIRChange = true;
2797  }
2798  }
2799 
2800  Worklist.Zap();
2801  return MadeIRChange;
2802 }
2803 
2804 /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding
2805 /// all reachable code to the worklist.
2806 ///
2807 /// This has a couple of tricks to make the code faster and more powerful. In
2808 /// particular, we constant fold and DCE instructions as we go, to avoid adding
2809 /// them to the worklist (this significantly speeds up instcombine on code where
2810 /// many instructions are dead or constant). Additionally, if we find a branch
2811 /// whose condition is a known constant, we only visit the reachable successors.
2812 ///
2815  InstCombineWorklist &ICWorklist,
2816  const TargetLibraryInfo *TLI) {
2817  bool MadeIRChange = false;
2819  Worklist.push_back(BB);
2820 
2821  SmallVector<Instruction*, 128> InstrsForInstCombineWorklist;
2822  DenseMap<ConstantExpr*, Constant*> FoldedConstants;
2823 
2824  do {
2825  BB = Worklist.pop_back_val();
2826 
2827  // We have now visited this block! If we've already been here, ignore it.
2828  if (!Visited.insert(BB).second)
2829  continue;
2830 
2831  for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
2832  Instruction *Inst = BBI++;
2833 
2834  // DCE instruction if trivially dead.
2835  if (isInstructionTriviallyDead(Inst, TLI)) {
2836  ++NumDeadInst;
2837  DEBUG(dbgs() << "IC: DCE: " << *Inst << '\n');
2838  Inst->eraseFromParent();
2839  continue;
2840  }
2841 
2842  // ConstantProp instruction if trivially constant.
2843  if (!Inst->use_empty() &&
2844  (Inst->getNumOperands() == 0 || isa<Constant>(Inst->getOperand(0))))
2845  if (Constant *C = ConstantFoldInstruction(Inst, DL, TLI)) {
2846  DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: "
2847  << *Inst << '\n');
2848  Inst->replaceAllUsesWith(C);
2849  ++NumConstProp;
2850  Inst->eraseFromParent();
2851  continue;
2852  }
2853 
2854  // See if we can constant fold its operands.
2855  for (User::op_iterator i = Inst->op_begin(), e = Inst->op_end(); i != e;
2856  ++i) {
2858  if (CE == nullptr)
2859  continue;
2860 
2861  Constant *&FoldRes = FoldedConstants[CE];
2862  if (!FoldRes)
2863  FoldRes = ConstantFoldConstantExpression(CE, DL, TLI);
2864  if (!FoldRes)
2865  FoldRes = CE;
2866 
2867  if (FoldRes != CE) {
2868  *i = FoldRes;
2869  MadeIRChange = true;
2870  }
2871  }
2872 
2873  InstrsForInstCombineWorklist.push_back(Inst);
2874  }
2875 
2876  // Recursively visit successors. If this is a branch or switch on a
2877  // constant, only visit the reachable successor.
2878  TerminatorInst *TI = BB->getTerminator();
2879  if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
2880  if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
2881  bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
2882  BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
2883  Worklist.push_back(ReachableBB);
2884  continue;
2885  }
2886  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
2887  if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
2888  // See if this is an explicit destination.
2889  for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
2890  i != e; ++i)
2891  if (i.getCaseValue() == Cond) {
2892  BasicBlock *ReachableBB = i.getCaseSuccessor();
2893  Worklist.push_back(ReachableBB);
2894  continue;
2895  }
2896 
2897  // Otherwise it is the default destination.
2898  Worklist.push_back(SI->getDefaultDest());
2899  continue;
2900  }
2901  }
2902 
2903  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
2904  Worklist.push_back(TI->getSuccessor(i));
2905  } while (!Worklist.empty());
2906 
2907  // Once we've found all of the instructions to add to instcombine's worklist,
2908  // add them in reverse order. This way instcombine will visit from the top
2909  // of the function down. This jives well with the way that it adds all uses
2910  // of instructions to the worklist after doing a transformation, thus avoiding
2911  // some N^2 behavior in pathological cases.
2912  ICWorklist.AddInitialGroup(&InstrsForInstCombineWorklist[0],
2913  InstrsForInstCombineWorklist.size());
2914 
2915  return MadeIRChange;
2916 }
2917 
2918 /// \brief Populate the IC worklist from a function, and prune any dead basic
2919 /// blocks discovered in the process.
2920 ///
2921 /// This also does basic constant propagation and other forward fixing to make
2922 /// the combiner itself run much faster.
2924  TargetLibraryInfo *TLI,
2925  InstCombineWorklist &ICWorklist) {
2926  bool MadeIRChange = false;
2927 
2928  // Do a depth-first traversal of the function, populate the worklist with
2929  // the reachable instructions. Ignore blocks that are not reachable. Keep
2930  // track of which blocks we visit.
2932  MadeIRChange |=
2933  AddReachableCodeToWorklist(F.begin(), DL, Visited, ICWorklist, TLI);
2934 
2935  // Do a quick scan over the function. If we find any blocks that are
2936  // unreachable, remove any instructions inside of them. This prevents
2937  // the instcombine code from having to deal with some bad special cases.
2938  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
2939  if (Visited.count(BB))
2940  continue;
2941 
2942  // Delete the instructions backwards, as it has a reduced likelihood of
2943  // having to update as many def-use and use-def chains.
2944  Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
2945  while (EndInst != BB->begin()) {
2946  // Delete the next to last instruction.
2947  BasicBlock::iterator I = EndInst;
2948  Instruction *Inst = --I;
2949  if (!Inst->use_empty())
2950  Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
2951  if (isa<LandingPadInst>(Inst)) {
2952  EndInst = Inst;
2953  continue;
2954  }
2955  if (!isa<DbgInfoIntrinsic>(Inst)) {
2956  ++NumDeadInst;
2957  MadeIRChange = true;
2958  }
2959  Inst->eraseFromParent();
2960  }
2961  }
2962 
2963  return MadeIRChange;
2964 }
2965 
2966 static bool
2968  AliasAnalysis *AA, AssumptionCache &AC,
2969  TargetLibraryInfo &TLI, DominatorTree &DT,
2970  LoopInfo *LI = nullptr) {
2971  // Minimizing size?
2972  bool MinimizeSize = F.hasFnAttribute(Attribute::MinSize);
2973  auto &DL = F.getParent()->getDataLayout();
2974 
2975  /// Builder - This is an IRBuilder that automatically inserts new
2976  /// instructions into the worklist when they are created.
2978  F.getContext(), TargetFolder(DL), InstCombineIRInserter(Worklist, &AC));
2979 
2980  // Lower dbg.declare intrinsics otherwise their value may be clobbered
2981  // by instcombiner.
2982  bool DbgDeclaresChanged = LowerDbgDeclare(F);
2983 
2984  // Iterate while there is work to do.
2985  int Iteration = 0;
2986  for (;;) {
2987  ++Iteration;
2988  DEBUG(dbgs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
2989  << F.getName() << "\n");
2990 
2991  bool Changed = false;
2992  if (prepareICWorklistFromFunction(F, DL, &TLI, Worklist))
2993  Changed = true;
2994 
2995  InstCombiner IC(Worklist, &Builder, MinimizeSize,
2996  AA, &AC, &TLI, &DT, DL, LI);
2997  if (IC.run())
2998  Changed = true;
2999 
3000  if (!Changed)
3001  break;
3002  }
3003 
3004  return DbgDeclaresChanged || Iteration > 1;
3005 }
3006 
3009  auto &AC = AM->getResult<AssumptionAnalysis>(F);
3010  auto &DT = AM->getResult<DominatorTreeAnalysis>(F);
3011  auto &TLI = AM->getResult<TargetLibraryAnalysis>(F);
3012 
3013  auto *LI = AM->getCachedResult<LoopAnalysis>(F);
3014 
3015  // FIXME: The AliasAnalysis is not yet supported in the new pass manager
3016  if (!combineInstructionsOverFunction(F, Worklist, nullptr, AC, TLI, DT, LI))
3017  // No changes, all analyses are preserved.
3018  return PreservedAnalyses::all();
3019 
3020  // Mark all the analyses that instcombine updates as preserved.
3021  // FIXME: Need a way to preserve CFG analyses here!
3022  PreservedAnalyses PA;
3024  return PA;
3025 }
3026 
3027 namespace {
3028 /// \brief The legacy pass manager's instcombine pass.
3029 ///
3030 /// This is a basic whole-function wrapper around the instcombine utility. It
3031 /// will try to combine all instructions in the function.
3032 class InstructionCombiningPass : public FunctionPass {
3033  InstCombineWorklist Worklist;
3034 
3035 public:
3036  static char ID; // Pass identification, replacement for typeid
3037 
3038  InstructionCombiningPass() : FunctionPass(ID) {
3040  }
3041 
3042  void getAnalysisUsage(AnalysisUsage &AU) const override;
3043  bool runOnFunction(Function &F) override;
3044 };
3045 }
3046 
3047 void InstructionCombiningPass::getAnalysisUsage(AnalysisUsage &AU) const {
3048  AU.setPreservesCFG();
3049  AU.addRequired<AliasAnalysis>();
3054 }
3055 
3056 bool InstructionCombiningPass::runOnFunction(Function &F) {
3057  if (skipOptnoneFunction(F))
3058  return false;
3059 
3060  // Required analyses.
3061  auto AA = &getAnalysis<AliasAnalysis>();
3062  auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
3063  auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
3064  auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
3065 
3066  // Optional analyses.
3067  auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
3068  auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
3069 
3070  return combineInstructionsOverFunction(F, Worklist, AA, AC, TLI, DT, LI);
3071 }
3072 
3074 INITIALIZE_PASS_BEGIN(InstructionCombiningPass, "instcombine",
3075  "Combine redundant instructions", false, false)
3080 INITIALIZE_PASS_END(InstructionCombiningPass, "instcombine",
3081  "Combine redundant instructions", false, false)
3082 
3083 // Initialization Routines
3086 }
3087 
3090 }
3091 
3093  return new InstructionCombiningPass();
3094 }
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
const NoneType None
Definition: None.h:23
ConstantDataVector - A vector constant whose element type is a simple 1/2/4/8-byte integer or float/d...
Definition: Constants.h:742
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the code necessary to compute th...
Definition: Local.h:193
ReturnInst - Return a value (possibly void), from a function.
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL, TargetLibraryInfo *TLI, InstCombineWorklist &ICWorklist)
Populate the IC worklist from a function, and prune any dead basic blocks discovered in the process...
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction, which must be an operator which supports these flags.
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition: InstrTypes.h:649
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:64
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
CaseIt case_end()
Returns a read/write iterator that points one past the last in the SwitchInst.
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:679
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1285
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:236
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
ExtractValueInst - This instruction extracts a struct member or array element value from an aggregate...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:223
iterator_range< CaseIt > cases()
cases - iteration adapter for range-for loops.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:446
static const Value * getFNegArgument(const Value *BinOp)
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1442
void LLVMInitializeInstCombine(LLVMPassRegistryRef R)
STATISTIC(NumFunctions,"Total number of functions")
bool isVolatile() const
isVolatile - Return true if this is a store to a volatile memory location.
Definition: Instructions.h:351
void swapSuccessors()
Swap the successors of this branch instruction.
bool isVolatile() const
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for this module.
Definition: PassManager.h:328
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
iterator end()
Definition: Function.h:459
static void ClearSubclassDataAfterReassociation(BinaryOperator &I)
Conservatively clears subclassOptionalData after a reassociation or commutation.
match_zero m_Zero()
Match an arbitrary zero/null constant.
Definition: PatternMatch.h:137
This file provides the primary interface to the instcombine pass.
INITIALIZE_PASS_BEGIN(InstructionCombiningPass,"instcombine","Combine redundant instructions", false, false) INITIALIZE_PASS_END(InstructionCombiningPass
unsigned getNumOperands() const
Definition: User.h:138
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property...
Definition: Operator.h:102
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:976
br_match m_UnconditionalBr(BasicBlock *&Succ)
Definition: PatternMatch.h:922
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:61
static Value * tryFactorization(InstCombiner::BuilderTy *Builder, const DataLayout &DL, BinaryOperator &I, Instruction::BinaryOps InnerOpcode, Value *A, Value *B, Value *C, Value *D)
This tries to simplify binary operations by factorizing out common terms (e.
CallInst - This class represents a function call, abstracting a target machine's calling convention...
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:276
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
Definition: InstrTypes.h:783
An immutable pass that tracks lazily created AssumptionCache objects.
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
gep_type_iterator gep_type_end(const User *GEP)
CaseIt case_begin()
Returns a read/write iterator that points to the first case in SwitchInst.
unsigned less or equal
Definition: InstrTypes.h:723
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:536
bool mayHaveSideEffects() const
mayHaveSideEffects - Return true if the instruction may have side effects.
Definition: Instruction.h:387
A cache of .assume calls within a function.
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Definition: APInt.cpp:1987
ShuffleVectorInst - This instruction constructs a fixed permutation of two input vectors.
static bool RightDistributesOverLeft(Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
RightDistributesOverLeft - Whether "(X LOp Y) ROp Z" is always equal to "(X ROp Z) LOp (Y ROp Z)"...
static bool isEquality(Predicate P)
isEquality - Return true if this predicate is either EQ or NE.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:166
F(f)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:472
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
Definition: Function.cpp:822
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:1990
Hexagon Common GEP
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2269
iv Induction Variable Users
Definition: IVUsers.cpp:43
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: Type.cpp:216
void reserve(size_type N)
Definition: SmallVector.h:401
Constant * ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstantExpression - Attempt to fold the constant expression using the specified DataLayo...
static bool MaintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C)
static Value * CreateBinOpAsGiven(BinaryOperator &Inst, Value *LHS, Value *RHS, InstCombiner::BuilderTy *B)
Creates node of binary operation with the same attributes as the specified one but with other operand...
unsigned getNumIndices() const
op_iterator op_begin()
Definition: User.h:183
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:2018
static LandingPadInst * Create(Type *RetTy, unsigned NumReservedClauses, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedClauses is a hint for the number of incoming clauses that this landingpad w...
Constant * getMask() const
Type * getPointerElementType() const
Definition: Type.h:366
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:923
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
bool hasIndices() const
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:178
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:242
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
bool swapOperands()
Exchange the two operands to this instruction.
ArrayRef< unsigned > getIndices() const
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:41
AnalysisUsage & addRequired()
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:475
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
static const Value * getNegArgument(const Value *BinOp)
Helper functions to extract the unary argument of a NEG, FNEG or NOT operation implemented via Sub...
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Definition: PatternMatch.h:732
bool isUnconditional() const
This class represents a conversion between pointers from one address space to another.
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
SelectInst - This class represents the LLVM 'select' instruction.
bool isIdenticalTo(const Instruction *I) const
isIdenticalTo - Return true if the specified instruction is exactly identical to the current one...
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:389
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:106
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:308
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
Definition: SmallVector.h:406
StructType - Class to represent struct types.
Definition: DerivedTypes.h:191
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
Type * getArrayElementType() const
Definition: Type.h:361
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1057
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:517
The core instruction combiner logic.
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:704
bool isSized(SmallPtrSetImpl< const Type * > *Visited=nullptr) const
isSized - Return true if it makes sense to take the size of this type.
Definition: Type.h:268
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1713
Number of individual test Apply this number of consecutive mutations to each input exit after the first new interesting input is found the minimized corpus is saved into the first input directory Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
void print(raw_ostream &O) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:3209
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:710
Instruction * clone() const
clone() - Create a copy of 'this' instruction that is identical in all ways except the following: ...
static bool shorter_filter(const Value *LHS, const Value *RHS)
Type * getPointerOperandType() const
getPointerOperandType - Method to return the pointer operand as a PointerType.
Definition: Instructions.h:971
not_match< LHS > m_Not(const LHS &L)
Definition: PatternMatch.h:854
Instruction * visitReturnInst(ReturnInst &RI)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:104
Instruction * visitBranchInst(BranchInst &BI)
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1727
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible. ...
Definition: Constants.cpp:1868
ConstantExpr - a constant value that is initialized with an expression using other constant values...
Definition: Constants.h:852
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Definition: Instructions.h:887
void setCleanup(bool V)
setCleanup - Indicate that this landingpad instruction is a cleanup.
bool mayReadFromMemory() const
mayReadFromMemory - Return true if this instruction may read memory.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:125
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
bool isAssociative() const
isAssociative - Return true if the instruction is associative:
ArrayType - Class to represent array types.
Definition: DerivedTypes.h:336
This instruction compares its operands according to the predicate given to the constructor.
BasicBlock * getSuccessor(unsigned i) const
This class represents a no-op cast from one type to another.
op_iterator idx_begin()
Definition: Instructions.h:954
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:75
bool isFloatingPointTy() const
isFloatingPointTy - Return true if this is one of the six floating point types
Definition: Type.h:159
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:32
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:866
unsigned getNumClauses() const
getNumClauses - Get the number of clauses for this landing pad.
StoreInst - an instruction for storing to memory.
Definition: Instructions.h:316
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
bool isArrayTy() const
isArrayTy - True if this is an instance of ArrayType.
Definition: Type.h:213
static bool AddReachableCodeToWorklist(BasicBlock *BB, const DataLayout &DL, SmallPtrSetImpl< BasicBlock * > &Visited, InstCombineWorklist &ICWorklist, const TargetLibraryInfo *TLI)
AddReachableCodeToWorklist - Walk the function in depth-first order, adding all reachable code to the...
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:432
FunctionPass * createInstructionCombiningPass()
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:256
iterator begin()
Definition: Function.h:457
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
Type * getElementType() const
Definition: DerivedTypes.h:323
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
bool isInBounds() const
isInBounds - Determine whether the GEP has the inbounds flag.
PointerType - Class to represent pointers.
Definition: DerivedTypes.h:449
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:107
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:491
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
Definition: InstrTypes.h:57
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
Definition: Instructions.h:830
bool isFilter(unsigned Idx) const
isFilter - Return 'true' if the clause and index Idx is a filter clause.
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2246
DataLayout * unwrap(LLVMTargetDataRef P)
Definition: DataLayout.h:465
LandingPadInst - The landingpad instruction holds all of the information necessary to generate correc...
static Instruction::BinaryOps getBinOpsForFactorization(Instruction::BinaryOps TopLevelOpcode, BinaryOperator *Op, Value *&LHS, Value *&RHS)
This function factors binary ops which can be combined using distributive laws.
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:35
An abstract set of preserved analyses following a transformation pass run.
Definition: PassManager.h:69
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
Definition: PatternMatch.h:180
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:476
Constant * stripPointerCasts()
Definition: Constant.h:170
bool run()
Run the combiner over the entire worklist until it is empty.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:932
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
Definition: InstrTypes.h:62
static Value * getIdentityValue(Instruction::BinaryOps OpCode, Value *V)
This function returns identity value for given opcode, which can be used to factor patterns like (X *...
BranchInst - Conditional or Unconditional Branch instruction.
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
bool isVectorTy() const
isVectorTy - True if this is an instance of VectorType.
Definition: Type.h:226
static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo)
isCatchAll - Return 'true' if the given typeinfo will match anything.
This is an important base class in LLVM.
Definition: Constant.h:41
const Value * getCondition() const
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1339
static bool LeftDistributesOverRight(Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
LeftDistributesOverRight - Whether "X LOp (Y ROp Z)" is always equal to "(X LOp Y) ROp (X LOp Z)"...
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1895
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:873
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:233
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:264
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1900
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Value * getRawDest() const
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:322
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:230
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:955
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
brc_match< Cond_t > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
Definition: PatternMatch.h:942
Represent the analysis usage information of a pass.
op_iterator op_end()
Definition: User.h:185
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:252
uint64_t getNumElements() const
Definition: DerivedTypes.h:352
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:697
Utility class for integer arithmetic operators which may exhibit overflow - Add, Sub, and Mul.
Definition: Operator.h:74
iterator insert(iterator where, NodeTy *New)
Definition: ilist.h:412
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1273
void addClause(Constant *ClauseVal)
Add a catch or filter clause to the landing pad.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
Value * getOperand(unsigned i) const
Definition: User.h:118
op_range operands()
Definition: User.h:191
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast or an AddrSpaceCast cast instruction.
bool isCommutative() const
isCommutative - Return true if the instruction is commutative:
Definition: Instruction.h:327
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', returning true if uncertain.
Definition: CFG.cpp:185
Class to represent integer types.
Definition: DerivedTypes.h:37
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2252
void setSourceElementType(Type *Ty)
Definition: Instructions.h:928
Constant * getAggregateElement(unsigned Elt) const
getAggregateElement - For aggregates (struct/array/vector) return the constant that corresponds to th...
Definition: Constants.cpp:250
#define INITIALIZE_AG_DEPENDENCY(depName)
Definition: PassSupport.h:72
static Value * FoldOperationIntoSelectOperand(Instruction &I, Value *SO, InstCombiner *IC)
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:416
InstCombineWorklist - This is the worklist management logic for InstCombine.
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1473
iterator erase(iterator I)
Definition: SmallVector.h:455
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:91
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:519
bool LowerDbgDeclare(Function &F)
LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set of llvm.dbg.value intrinsics.
Definition: Local.cpp:1028
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
const Value * getTrueValue() const
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:854
static Constant * getIntegerValue(Type *Ty, const APInt &V)
getIntegerValue - Return the value for an integer or pointer constant, or a vector thereof...
Definition: Constants.cpp:213
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
Definition: Instructions.h:938
neg_match< LHS > m_Neg(const LHS &L)
Match an integer negate.
Definition: PatternMatch.h:877
bool isConditional() const
SequentialType - This is the superclass of the array, pointer and vector type classes.
Definition: DerivedTypes.h:310
A function analysis which provides an AssumptionCache.
BinaryOps getOpcode() const
Definition: InstrTypes.h:323
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:304
static InvokeInst * Create(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
MemIntrinsic - This is the common base class for memset/memcpy/memmove.
static PointerType * getInt1PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:279
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
struct LLVMOpaquePassRegistry * LLVMPassRegistryRef
Definition: Core.h:119
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB)
Translate PHI node to its predecessor from the given basic block.
Definition: Value.cpp:511
iterator end()
Definition: BasicBlock.h:233
unsigned getScalarSizeInBits() const LLVM_READONLY
getScalarSizeInBits - If this is a vector type, return the getPrimitiveSizeInBits value for the eleme...
Definition: Type.cpp:139
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:216
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
Instruction * user_back()
user_back - Specialize the methods defined in Value, as we know that an instruction can only be used ...
Definition: Instruction.h:69
Provides information about what library functions are available for the current target.
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
Definition: Operator.h:421
Value * SimplifyGEPInst(ArrayRef< Value * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can fold the result...
uint64_t getSizeInBytes() const
Definition: DataLayout.h:481
SequentialType * getType() const
Definition: Instructions.h:922
PassT::Result & getResult(IRUnitT &IR)
Get the result of an analysis pass for this module.
Definition: PassManager.h:311
Value * stripPointerCasts()
Strip off pointer casts, all-zero GEPs, and aliases.
Definition: Value.cpp:458
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it...
Definition: DataLayout.cpp:74
Instruction * visitFree(CallInst &FI)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:161
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
int32_t exactLogBase2() const
Definition: APInt.h:1559
const BasicBlock & getEntryBlock() const
Definition: Function.h:442
void initializeInstCombine(PassRegistry &)
initializeInstCombine - Initialize all passes linked into the InstCombine library.
bool isNullValue() const
isNullValue - Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:75
bool isExact() const
Determine whether the exact flag is set.
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:530
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
Definition: InstrTypes.h:765
void setOperand(unsigned i, Value *Val)
Definition: User.h:122
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:576
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
signed less or equal
Definition: InstrTypes.h:727
VectorType - Class to represent vector types.
Definition: DerivedTypes.h:362
static bool isNeg(const Value *V)
Check if the given Value is a NEG, FNeg, or NOT instruction.
ConstantArray - Constant Array Declarations.
Definition: Constants.h:356
Class for arbitrary precision integers.
Definition: APInt.h:73
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
Definition: Type.h:193
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
Definition: PatternMatch.h:795
iterator_range< user_iterator > users()
Definition: Value.h:300
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:211
Instruction * visitSwitchInst(SwitchInst &SI)
Value * SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
SimplifyBinOp - Given operands for a BinaryOperator, see if we can fold the result.
static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock)
TryToSinkInstruction - Try to move the specified instruction from its current block into the beginnin...
Instruction * visitExtractValueInst(ExtractValueInst &EV)
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1591
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
idx_iterator idx_end() const
Value * getCondition() const
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:361
CompositeType - Common super class of ArrayType, StructType, PointerType and VectorType.
Definition: DerivedTypes.h:148
void AddInitialGroup(Instruction *const *List, unsigned NumEntries)
AddInitialGroup - Add the specified batch of stuff in reverse order.
const Type * getScalarType() const LLVM_READONLY
getScalarType - If this is a vector type, return the element type, otherwise return 'this'...
Definition: Type.cpp:51
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1890
bool isStructTy() const
isStructTy - True if this is an instance of StructType.
Definition: Type.h:209
Instruction * visitLandingPadInst(LandingPadInst &LI)
use_iterator use_begin()
Definition: Value.h:279
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2239
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
unsigned countLeadingOnes() const
Count the number of leading one bits.
Definition: APInt.cpp:722
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
Value * getCondition() const
void emplace_back(ArgTypes &&...Args)
Definition: SmallVector.h:652
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:367
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:217
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
void setCondition(Value *V)
Constant * getPersonalityFn() const
Definition: Function.h:133
Combine redundant instructions
unsigned greater or equal
Definition: InstrTypes.h:721
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
bool isCatch(unsigned Idx) const
isCatch - Return 'true' if the clause and index Idx is a catch clause.
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:311
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:705
PreservedAnalyses run(Function &F, AnalysisManager< Function > *AM)
static ArrayType * get(Type *ElementType, uint64_t NumElements)
ArrayType::get - This static method is the primary way to construct an ArrayType. ...
Definition: Type.cpp:686
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2329
VectorType * getType() const
getType - Overload to return most specific vector type.
void preserve()
Mark a particular pass as preserved, adding it to the set.
Definition: PassManager.h:98
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:2005
size_t size() const
Definition: BasicBlock.h:241
Value * SimplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for an ExtractValueInst, see if we can fold the result.
void initializeInstructionCombiningPassPass(PassRegistry &)
void setCondition(Value *V)
Analysis pass providing the TargetLibraryInfo.
SwitchInst - Multiway switch.
bool use_empty() const
Definition: Value.h:275
static bool isAllocSiteRemovable(Instruction *AI, SmallVectorImpl< WeakVH > &Users, const TargetLibraryInfo *TLI)
user_iterator user_begin()
Definition: Value.h:294
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:465
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
getPrimitiveSizeInBits - Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:121
static bool combineInstructionsOverFunction(Function &F, InstCombineWorklist &Worklist, AliasAnalysis *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT, LoopInfo *LI=nullptr)
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
isInstructionTriviallyDead - Return true if the result produced by the instruction is not used...
Definition: Local.cpp:282
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
This file provides internal interfaces used to implement the InstCombine.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:112
An IRBuilder inserter that adds new instructions to the instcombine worklist.
void clearSubclassOptionalData()
Clear the optional flags contained in this value.
Definition: Value.h:374
void moveBefore(Instruction *MovePos)
moveBefore - Unlink this instruction from its current basic block and insert it into the basic block ...
Definition: Instruction.cpp:89
static Instruction * tryToMoveFreeBeforeNullTest(CallInst &FI)
Move the call to free before a NULL test.
InvokeInst - Invoke instruction.
#define DEBUG(X)
Definition: Debug.h:92
Instruction * visitGetElementPtrInst(GetElementPtrInst &GEP)
bool isCleanup() const
isCleanup - Return 'true' if this landingpad instruction is a cleanup.
Combine redundant false
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const Value * getFalseValue() const
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:164
unsigned greater than
Definition: InstrTypes.h:720
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:41
A generic analysis pass manager with lazy running and caching of results.
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
isSafeToSpeculativelyExecute - Return true if the instruction does not have any effects besides calcu...
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
idx_iterator idx_begin() const
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:194
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
static bool shouldMergeGEPs(GEPOperator &GEP, GEPOperator &Src)
static void getShuffleMask(Constant *Mask, SmallVectorImpl< int > &Result)
getShuffleMask - Return the full mask for this instruction, where each element is the element number ...
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:702
Value * getPointerOperand()
Definition: Instructions.h:409
Instruction * visitAllocSite(Instruction &FI)
const BasicBlock * getParent() const
Definition: Instruction.h:72
signed greater or equal
Definition: InstrTypes.h:725
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:37
InsertValueInst - This instruction inserts a struct field of array element value into an aggregate va...
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Definition: PatternMatch.h:726
gep_type_iterator gep_type_begin(const User *GEP)
user_iterator user_end()
Definition: Value.h:296
Function must be optimized for size first.
Definition: Attributes.h:80