Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1 | //===- InstructionSimplify.cpp - Fold instruction operands ----------------===// |
| 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 | // This file implements routines for folding instructions into simpler forms |
Duncan Sands | 4cd2ad1 | 2010-11-23 10:50:08 +0000 | [diff] [blame] | 11 | // that do not require creating new instructions. This does constant folding |
| 12 | // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either |
| 13 | // returning a constant ("and i32 %x, 0" -> "0") or an already existing value |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 14 | // ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been |
| 15 | // simplified: This is usually true and assuming it simplifies the logic (if |
| 16 | // they have not been simplified then results are correct but maybe suboptimal). |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 20 | #define DEBUG_TYPE "instsimplify" |
Jay Foad | 562b84b | 2011-04-11 09:35:34 +0000 | [diff] [blame] | 21 | #include "llvm/Operator.h" |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/InstructionSimplify.h" |
| 24 | #include "llvm/Analysis/ConstantFolding.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/Dominators.h" |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ValueTracking.h" |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 28 | #include "llvm/Support/PatternMatch.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ValueHandle.h" |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 32 | using namespace llvm::PatternMatch; |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 34 | enum { RecursionLimit = 3 }; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 35 | |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 36 | STATISTIC(NumExpand, "Number of expansions"); |
| 37 | STATISTIC(NumFactor , "Number of factorizations"); |
| 38 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 39 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 40 | static Value *SimplifyAndInst(Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 41 | const TargetLibraryInfo *, const DominatorTree *, |
| 42 | unsigned); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 43 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 44 | const TargetLibraryInfo *, const DominatorTree *, |
| 45 | unsigned); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 46 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 47 | const TargetLibraryInfo *, const DominatorTree *, |
| 48 | unsigned); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 49 | static Value *SimplifyOrInst(Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 50 | const TargetLibraryInfo *, const DominatorTree *, |
| 51 | unsigned); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 52 | static Value *SimplifyXorInst(Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 53 | const TargetLibraryInfo *, const DominatorTree *, |
| 54 | unsigned); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 55 | |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 56 | /// getFalse - For a boolean type, or a vector of boolean type, return false, or |
| 57 | /// a vector with every element false, as appropriate for the type. |
| 58 | static Constant *getFalse(Type *Ty) { |
Nick Lewycky | 66d004e | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 59 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 60 | "Expected i1 type or a vector of i1!"); |
| 61 | return Constant::getNullValue(Ty); |
| 62 | } |
| 63 | |
| 64 | /// getTrue - For a boolean type, or a vector of boolean type, return true, or |
| 65 | /// a vector with every element true, as appropriate for the type. |
| 66 | static Constant *getTrue(Type *Ty) { |
Nick Lewycky | 66d004e | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 67 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 68 | "Expected i1 type or a vector of i1!"); |
| 69 | return Constant::getAllOnesValue(Ty); |
| 70 | } |
| 71 | |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 72 | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
| 73 | static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, |
| 74 | Value *RHS) { |
| 75 | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
| 76 | if (!Cmp) |
| 77 | return false; |
| 78 | CmpInst::Predicate CPred = Cmp->getPredicate(); |
| 79 | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
| 80 | if (CPred == Pred && CLHS == LHS && CRHS == RHS) |
| 81 | return true; |
| 82 | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && |
| 83 | CRHS == LHS; |
| 84 | } |
| 85 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 86 | /// ValueDominatesPHI - Does the given value dominate the specified phi node? |
| 87 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 88 | Instruction *I = dyn_cast<Instruction>(V); |
| 89 | if (!I) |
| 90 | // Arguments and constants dominate all instructions. |
| 91 | return true; |
| 92 | |
| 93 | // If we have a DominatorTree then do a precise test. |
| 94 | if (DT) |
| 95 | return DT->dominates(I, P); |
| 96 | |
| 97 | // Otherwise, if the instruction is in the entry block, and is not an invoke, |
| 98 | // then it obviously dominates all phi nodes. |
| 99 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
| 100 | !isa<InvokeInst>(I)) |
| 101 | return true; |
| 102 | |
| 103 | return false; |
| 104 | } |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 105 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 106 | /// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning |
| 107 | /// it into "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is |
| 108 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 109 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 110 | /// Returns the simplified value, or null if no simplification was performed. |
| 111 | static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 112 | unsigned OpcToExpand, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 113 | const TargetLibraryInfo *TLI, const DominatorTree *DT, |
| 114 | unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 115 | Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 116 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 117 | if (!MaxRecurse--) |
| 118 | return 0; |
| 119 | |
| 120 | // Check whether the expression has the form "(A op' B) op C". |
| 121 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 122 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 123 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 124 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 125 | // Do "A op C" and "B op C" both simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 126 | if (Value *L = SimplifyBinOp(Opcode, A, C, TD, TLI, DT, MaxRecurse)) |
| 127 | if (Value *R = SimplifyBinOp(Opcode, B, C, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 128 | // They do! Return "L op' R" if it simplifies or is already available. |
| 129 | // If "L op' R" equals "A op' B" then "L op' R" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 130 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 131 | && L == B && R == A)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 132 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 133 | return LHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 134 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 135 | // Otherwise return "L op' R" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 136 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, TLI, DT, |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 137 | MaxRecurse)) { |
| 138 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 139 | return V; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 140 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | // Check whether the expression has the form "A op (B op' C)". |
| 145 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 146 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 147 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 148 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 149 | // Do "A op B" and "A op C" both simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 150 | if (Value *L = SimplifyBinOp(Opcode, A, B, TD, TLI, DT, MaxRecurse)) |
| 151 | if (Value *R = SimplifyBinOp(Opcode, A, C, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 152 | // They do! Return "L op' R" if it simplifies or is already available. |
| 153 | // If "L op' R" equals "B op' C" then "L op' R" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 154 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 155 | && L == C && R == B)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 156 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 157 | return RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 158 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 159 | // Otherwise return "L op' R" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 160 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, TLI, DT, |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 161 | MaxRecurse)) { |
| 162 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 163 | return V; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 164 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /// FactorizeBinOp - Simplify "LHS Opcode RHS" by factorizing out a common term |
| 172 | /// using the operation OpCodeToExtract. For example, when Opcode is Add and |
| 173 | /// OpCodeToExtract is Mul then this tries to turn "(A*B)+(A*C)" into "A*(B+C)". |
| 174 | /// Returns the simplified value, or null if no simplification was performed. |
| 175 | static Value *FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 176 | unsigned OpcToExtract, const TargetData *TD, |
| 177 | const TargetLibraryInfo *TLI, |
| 178 | const DominatorTree *DT, |
| 179 | unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 180 | Instruction::BinaryOps OpcodeToExtract = (Instruction::BinaryOps)OpcToExtract; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 181 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 182 | if (!MaxRecurse--) |
| 183 | return 0; |
| 184 | |
| 185 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 186 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 187 | |
| 188 | if (!Op0 || Op0->getOpcode() != OpcodeToExtract || |
| 189 | !Op1 || Op1->getOpcode() != OpcodeToExtract) |
| 190 | return 0; |
| 191 | |
| 192 | // The expression has the form "(A op' B) op (C op' D)". |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 193 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1); |
| 194 | Value *C = Op1->getOperand(0), *D = Op1->getOperand(1); |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 195 | |
| 196 | // Use left distributivity, i.e. "X op' (Y op Z) = (X op' Y) op (X op' Z)". |
| 197 | // Does the instruction have the form "(A op' B) op (A op' D)" or, in the |
| 198 | // commutative case, "(A op' B) op (C op' A)"? |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 199 | if (A == C || (Instruction::isCommutative(OpcodeToExtract) && A == D)) { |
| 200 | Value *DD = A == C ? D : C; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 201 | // Form "A op' (B op DD)" if it simplifies completely. |
| 202 | // Does "B op DD" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 203 | if (Value *V = SimplifyBinOp(Opcode, B, DD, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 204 | // It does! Return "A op' V" if it simplifies or is already available. |
Duncan Sands | 1cd05bb | 2010-12-22 17:15:25 +0000 | [diff] [blame] | 205 | // If V equals B then "A op' V" is just the LHS. If V equals DD then |
| 206 | // "A op' V" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 207 | if (V == B || V == DD) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 208 | ++NumFactor; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 209 | return V == B ? LHS : RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 210 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 211 | // Otherwise return "A op' V" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 212 | if (Value *W = SimplifyBinOp(OpcodeToExtract, A, V, TD, TLI, DT, |
| 213 | MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 214 | ++NumFactor; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 215 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 216 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | // Use right distributivity, i.e. "(X op Y) op' Z = (X op' Z) op (Y op' Z)". |
| 221 | // Does the instruction have the form "(A op' B) op (C op' B)" or, in the |
| 222 | // commutative case, "(A op' B) op (B op' D)"? |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 223 | if (B == D || (Instruction::isCommutative(OpcodeToExtract) && B == C)) { |
| 224 | Value *CC = B == D ? C : D; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 225 | // Form "(A op CC) op' B" if it simplifies completely.. |
| 226 | // Does "A op CC" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 227 | if (Value *V = SimplifyBinOp(Opcode, A, CC, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 228 | // It does! Return "V op' B" if it simplifies or is already available. |
Duncan Sands | 1cd05bb | 2010-12-22 17:15:25 +0000 | [diff] [blame] | 229 | // If V equals A then "V op' B" is just the LHS. If V equals CC then |
| 230 | // "V op' B" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 231 | if (V == A || V == CC) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 232 | ++NumFactor; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 233 | return V == A ? LHS : RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 234 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 235 | // Otherwise return "V op' B" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 236 | if (Value *W = SimplifyBinOp(OpcodeToExtract, V, B, TD, TLI, DT, |
| 237 | MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 238 | ++NumFactor; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 239 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 240 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | /// SimplifyAssociativeBinOp - Generic simplifications for associative binary |
| 248 | /// operations. Returns the simpler value, or null if none was found. |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 249 | static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 250 | const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 251 | const TargetLibraryInfo *TLI, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 252 | const DominatorTree *DT, |
| 253 | unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 254 | Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 255 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 256 | |
| 257 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 258 | if (!MaxRecurse--) |
| 259 | return 0; |
| 260 | |
| 261 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 262 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 263 | |
| 264 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 265 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 266 | Value *A = Op0->getOperand(0); |
| 267 | Value *B = Op0->getOperand(1); |
| 268 | Value *C = RHS; |
| 269 | |
| 270 | // Does "B op C" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 271 | if (Value *V = SimplifyBinOp(Opcode, B, C, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 272 | // It does! Return "A op V" if it simplifies or is already available. |
| 273 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 274 | if (V == B) return LHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 275 | // Otherwise return "A op V" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 276 | if (Value *W = SimplifyBinOp(Opcode, A, V, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 277 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 278 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 279 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 284 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 285 | Value *A = LHS; |
| 286 | Value *B = Op1->getOperand(0); |
| 287 | Value *C = Op1->getOperand(1); |
| 288 | |
| 289 | // Does "A op B" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 290 | if (Value *V = SimplifyBinOp(Opcode, A, B, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 291 | // It does! Return "V op C" if it simplifies or is already available. |
| 292 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 293 | if (V == B) return RHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 294 | // Otherwise return "V op C" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 295 | if (Value *W = SimplifyBinOp(Opcode, V, C, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 296 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 297 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 298 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | // The remaining transforms require commutativity as well as associativity. |
| 303 | if (!Instruction::isCommutative(Opcode)) |
| 304 | return 0; |
| 305 | |
| 306 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 307 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 308 | Value *A = Op0->getOperand(0); |
| 309 | Value *B = Op0->getOperand(1); |
| 310 | Value *C = RHS; |
| 311 | |
| 312 | // Does "C op A" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 313 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 314 | // It does! Return "V op B" if it simplifies or is already available. |
| 315 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 316 | if (V == A) return LHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 317 | // Otherwise return "V op B" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 318 | if (Value *W = SimplifyBinOp(Opcode, V, B, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 319 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 320 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 321 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 326 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 327 | Value *A = LHS; |
| 328 | Value *B = Op1->getOperand(0); |
| 329 | Value *C = Op1->getOperand(1); |
| 330 | |
| 331 | // Does "C op A" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 332 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 333 | // It does! Return "B op V" if it simplifies or is already available. |
| 334 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 335 | if (V == C) return RHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 336 | // Otherwise return "B op V" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 337 | if (Value *W = SimplifyBinOp(Opcode, B, V, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 338 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 339 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 340 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 347 | /// ThreadBinOpOverSelect - In the case of a binary operation with a select |
| 348 | /// instruction as an operand, try to simplify the binop by seeing whether |
| 349 | /// evaluating it on both branches of the select results in the same value. |
| 350 | /// Returns the common value if so, otherwise returns null. |
| 351 | static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 352 | const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 353 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 354 | const DominatorTree *DT, |
| 355 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 356 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 357 | if (!MaxRecurse--) |
| 358 | return 0; |
| 359 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 360 | SelectInst *SI; |
| 361 | if (isa<SelectInst>(LHS)) { |
| 362 | SI = cast<SelectInst>(LHS); |
| 363 | } else { |
| 364 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 365 | SI = cast<SelectInst>(RHS); |
| 366 | } |
| 367 | |
| 368 | // Evaluate the BinOp on the true and false branches of the select. |
| 369 | Value *TV; |
| 370 | Value *FV; |
| 371 | if (SI == LHS) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 372 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, TLI, DT, MaxRecurse); |
| 373 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 374 | } else { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 375 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, TLI, DT, MaxRecurse); |
| 376 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, TLI, DT, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Duncan Sands | 7cf85e7 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 379 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 380 | // If they both failed to simplify then return null. |
| 381 | if (TV == FV) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 382 | return TV; |
| 383 | |
| 384 | // If one branch simplified to undef, return the other one. |
| 385 | if (TV && isa<UndefValue>(TV)) |
| 386 | return FV; |
| 387 | if (FV && isa<UndefValue>(FV)) |
| 388 | return TV; |
| 389 | |
| 390 | // If applying the operation did not change the true and false select values, |
| 391 | // then the result of the binop is the select itself. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 392 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 393 | return SI; |
| 394 | |
| 395 | // If one branch simplified and the other did not, and the simplified |
| 396 | // value is equal to the unsimplified one, return the simplified value. |
| 397 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 398 | if ((FV && !TV) || (TV && !FV)) { |
| 399 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 400 | // same as the original operation. |
| 401 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 402 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 403 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 404 | // We already know that "op" is the same as for the simplified value. See |
| 405 | // if the operands match too. If so, return the simplified value. |
| 406 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 407 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 408 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 409 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 410 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 411 | return Simplified; |
| 412 | if (Simplified->isCommutative() && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 413 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 414 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 415 | return Simplified; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | /// ThreadCmpOverSelect - In the case of a comparison with a select instruction, |
| 423 | /// try to simplify the comparison by seeing whether both branches of the select |
| 424 | /// result in the same value. Returns the common value if so, otherwise returns |
| 425 | /// null. |
| 426 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 427 | Value *RHS, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 428 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 429 | const DominatorTree *DT, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 430 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 431 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 432 | if (!MaxRecurse--) |
| 433 | return 0; |
| 434 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 435 | // Make sure the select is on the LHS. |
| 436 | if (!isa<SelectInst>(LHS)) { |
| 437 | std::swap(LHS, RHS); |
| 438 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 439 | } |
| 440 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 441 | SelectInst *SI = cast<SelectInst>(LHS); |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 442 | Value *Cond = SI->getCondition(); |
| 443 | Value *TV = SI->getTrueValue(); |
| 444 | Value *FV = SI->getFalseValue(); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 445 | |
Duncan Sands | 50ca4d3 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 446 | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 447 | // Does "cmp TV, RHS" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 448 | Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 449 | if (TCmp == Cond) { |
| 450 | // It not only simplified, it simplified to the select condition. Replace |
| 451 | // it with 'true'. |
| 452 | TCmp = getTrue(Cond->getType()); |
| 453 | } else if (!TCmp) { |
| 454 | // It didn't simplify. However if "cmp TV, RHS" is equal to the select |
| 455 | // condition then we can replace it with 'true'. Otherwise give up. |
| 456 | if (!isSameCompare(Cond, Pred, TV, RHS)) |
| 457 | return 0; |
| 458 | TCmp = getTrue(Cond->getType()); |
Duncan Sands | 50ca4d3 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 461 | // Does "cmp FV, RHS" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 462 | Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 463 | if (FCmp == Cond) { |
| 464 | // It not only simplified, it simplified to the select condition. Replace |
| 465 | // it with 'false'. |
| 466 | FCmp = getFalse(Cond->getType()); |
| 467 | } else if (!FCmp) { |
| 468 | // It didn't simplify. However if "cmp FV, RHS" is equal to the select |
| 469 | // condition then we can replace it with 'false'. Otherwise give up. |
| 470 | if (!isSameCompare(Cond, Pred, FV, RHS)) |
| 471 | return 0; |
| 472 | FCmp = getFalse(Cond->getType()); |
| 473 | } |
| 474 | |
| 475 | // If both sides simplified to the same value, then use it as the result of |
| 476 | // the original comparison. |
| 477 | if (TCmp == FCmp) |
| 478 | return TCmp; |
| 479 | // If the false value simplified to false, then the result of the compare |
| 480 | // is equal to "Cond && TCmp". This also catches the case when the false |
| 481 | // value simplified to false and the true value to true, returning "Cond". |
| 482 | if (match(FCmp, m_Zero())) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 483 | if (Value *V = SimplifyAndInst(Cond, TCmp, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 484 | return V; |
| 485 | // If the true value simplified to true, then the result of the compare |
| 486 | // is equal to "Cond || FCmp". |
| 487 | if (match(TCmp, m_One())) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 488 | if (Value *V = SimplifyOrInst(Cond, FCmp, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 489 | return V; |
| 490 | // Finally, if the false value simplified to true and the true value to |
| 491 | // false, then the result of the compare is equal to "!Cond". |
| 492 | if (match(FCmp, m_One()) && match(TCmp, m_Zero())) |
| 493 | if (Value *V = |
| 494 | SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 495 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 496 | return V; |
| 497 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 501 | /// ThreadBinOpOverPHI - In the case of a binary operation with an operand that |
| 502 | /// is a PHI instruction, try to simplify the binop by seeing whether evaluating |
| 503 | /// it on the incoming phi values yields the same result for every value. If so |
| 504 | /// returns the common value, otherwise returns null. |
| 505 | static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 506 | const TargetData *TD, |
| 507 | const TargetLibraryInfo *TLI, |
| 508 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 509 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 510 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 511 | if (!MaxRecurse--) |
| 512 | return 0; |
| 513 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 514 | PHINode *PI; |
| 515 | if (isa<PHINode>(LHS)) { |
| 516 | PI = cast<PHINode>(LHS); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 517 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
| 518 | if (!ValueDominatesPHI(RHS, PI, DT)) |
| 519 | return 0; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 520 | } else { |
| 521 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 522 | PI = cast<PHINode>(RHS); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 523 | // Bail out if LHS and the phi may be mutually interdependent due to a loop. |
| 524 | if (!ValueDominatesPHI(LHS, PI, DT)) |
| 525 | return 0; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // Evaluate the BinOp on the incoming phi values. |
| 529 | Value *CommonValue = 0; |
| 530 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 531 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 532 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 533 | if (Incoming == PI) continue; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 534 | Value *V = PI == LHS ? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 535 | SimplifyBinOp(Opcode, Incoming, RHS, TD, TLI, DT, MaxRecurse) : |
| 536 | SimplifyBinOp(Opcode, LHS, Incoming, TD, TLI, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 537 | // If the operation failed to simplify, or simplified to a different value |
| 538 | // to previously, then give up. |
| 539 | if (!V || (CommonValue && V != CommonValue)) |
| 540 | return 0; |
| 541 | CommonValue = V; |
| 542 | } |
| 543 | |
| 544 | return CommonValue; |
| 545 | } |
| 546 | |
| 547 | /// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try |
| 548 | /// try to simplify the comparison by seeing whether comparing with all of the |
| 549 | /// incoming phi values yields the same result every time. If so returns the |
| 550 | /// common result, otherwise returns null. |
| 551 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 552 | const TargetData *TD, |
| 553 | const TargetLibraryInfo *TLI, |
| 554 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 555 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 556 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 557 | if (!MaxRecurse--) |
| 558 | return 0; |
| 559 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 560 | // Make sure the phi is on the LHS. |
| 561 | if (!isa<PHINode>(LHS)) { |
| 562 | std::swap(LHS, RHS); |
| 563 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 564 | } |
| 565 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 566 | PHINode *PI = cast<PHINode>(LHS); |
| 567 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 568 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
| 569 | if (!ValueDominatesPHI(RHS, PI, DT)) |
| 570 | return 0; |
| 571 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 572 | // Evaluate the BinOp on the incoming phi values. |
| 573 | Value *CommonValue = 0; |
| 574 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 575 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 576 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 577 | if (Incoming == PI) continue; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 578 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 579 | // If the operation failed to simplify, or simplified to a different value |
| 580 | // to previously, then give up. |
| 581 | if (!V || (CommonValue && V != CommonValue)) |
| 582 | return 0; |
| 583 | CommonValue = V; |
| 584 | } |
| 585 | |
| 586 | return CommonValue; |
| 587 | } |
| 588 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 589 | /// SimplifyAddInst - Given operands for an Add, see if we can |
| 590 | /// fold the result. If not, this returns null. |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 591 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 592 | const TargetData *TD, |
| 593 | const TargetLibraryInfo *TLI, |
| 594 | const DominatorTree *DT, |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 595 | unsigned MaxRecurse) { |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 596 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 597 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 598 | Constant *Ops[] = { CLHS, CRHS }; |
| 599 | return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 600 | Ops, TD, TLI); |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 601 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 602 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 603 | // Canonicalize the constant to the RHS. |
| 604 | std::swap(Op0, Op1); |
| 605 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 606 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 607 | // X + undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 608 | if (match(Op1, m_Undef())) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 609 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 610 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 611 | // X + 0 -> X |
| 612 | if (match(Op1, m_Zero())) |
| 613 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 614 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 615 | // X + (Y - X) -> Y |
| 616 | // (Y - X) + X -> Y |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 617 | // Eg: X + -X -> 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 618 | Value *Y = 0; |
| 619 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 620 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 621 | return Y; |
| 622 | |
| 623 | // X + ~X -> -1 since ~X = -X-1 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 624 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 625 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 626 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 627 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 628 | /// i1 add -> xor. |
Duncan Sands | 75d289e | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 629 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 630 | if (Value *V = SimplifyXorInst(Op0, Op1, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 07f30fb | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 631 | return V; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 632 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 633 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 634 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 635 | MaxRecurse)) |
| 636 | return V; |
| 637 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 638 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 639 | if (Value *V = FactorizeBinOp(Instruction::Add, Op0, Op1, Instruction::Mul, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 640 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 641 | return V; |
| 642 | |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 643 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 644 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 645 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 646 | // only if B and C are equal. If B and C are equal then (since we assume |
| 647 | // that operands have already been simplified) "select(cond, B, C)" should |
| 648 | // have been simplified to the common value of B and C already. Analysing |
| 649 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 650 | // for threading over phi nodes. |
| 651 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 655 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 656 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 657 | const DominatorTree *DT) { |
| 658 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, TD, TLI, DT, RecursionLimit); |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 661 | /// SimplifySubInst - Given operands for a Sub, see if we can |
| 662 | /// fold the result. If not, this returns null. |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 663 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 664 | const TargetData *TD, |
| 665 | const TargetLibraryInfo *TLI, |
| 666 | const DominatorTree *DT, |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 667 | unsigned MaxRecurse) { |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 668 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) |
| 669 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 670 | Constant *Ops[] = { CLHS, CRHS }; |
| 671 | return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 672 | Ops, TD, TLI); |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // X - undef -> undef |
| 676 | // undef - X -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 677 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 678 | return UndefValue::get(Op0->getType()); |
| 679 | |
| 680 | // X - 0 -> X |
| 681 | if (match(Op1, m_Zero())) |
| 682 | return Op0; |
| 683 | |
| 684 | // X - X -> 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 685 | if (Op0 == Op1) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 686 | return Constant::getNullValue(Op0->getType()); |
| 687 | |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 688 | // (X*2) - X -> X |
| 689 | // (X<<1) - X -> X |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 690 | Value *X = 0; |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 691 | if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) || |
| 692 | match(Op0, m_Shl(m_Specific(Op1), m_One()))) |
| 693 | return Op1; |
| 694 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 695 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 696 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
| 697 | Value *Y = 0, *Z = Op1; |
| 698 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 699 | // See if "V === Y - Z" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 700 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 701 | // It does! Now see if "X + V" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 702 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, TD, TLI, DT, |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 703 | MaxRecurse-1)) { |
| 704 | // It does, we successfully reassociated! |
| 705 | ++NumReassoc; |
| 706 | return W; |
| 707 | } |
| 708 | // See if "V === X - Z" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 709 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 710 | // It does! Now see if "Y + V" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 711 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, TD, TLI, DT, |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 712 | MaxRecurse-1)) { |
| 713 | // It does, we successfully reassociated! |
| 714 | ++NumReassoc; |
| 715 | return W; |
| 716 | } |
| 717 | } |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 718 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 719 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 720 | // For example, X - (X + 1) -> -1 |
| 721 | X = Op0; |
| 722 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 723 | // See if "V === X - Y" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 724 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 725 | // It does! Now see if "V - Z" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 726 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, TD, TLI, DT, |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 727 | MaxRecurse-1)) { |
| 728 | // It does, we successfully reassociated! |
| 729 | ++NumReassoc; |
| 730 | return W; |
| 731 | } |
| 732 | // See if "V === X - Z" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 733 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 734 | // It does! Now see if "V - Y" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 735 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, TD, TLI, DT, |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 736 | MaxRecurse-1)) { |
| 737 | // It does, we successfully reassociated! |
| 738 | ++NumReassoc; |
| 739 | return W; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 744 | // For example, X - (X - Y) -> Y. |
| 745 | Z = Op0; |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 746 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 747 | // See if "V === Z - X" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 748 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 749 | // It does! Now see if "V + Y" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 750 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, TLI, DT, |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 751 | MaxRecurse-1)) { |
| 752 | // It does, we successfully reassociated! |
| 753 | ++NumReassoc; |
| 754 | return W; |
| 755 | } |
| 756 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 757 | // Mul distributes over Sub. Try some generic simplifications based on this. |
| 758 | if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 759 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 760 | return V; |
| 761 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 762 | // i1 sub -> xor. |
| 763 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 764 | if (Value *V = SimplifyXorInst(Op0, Op1, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 765 | return V; |
| 766 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 767 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 768 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 769 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 770 | // only if B and C are equal. If B and C are equal then (since we assume |
| 771 | // that operands have already been simplified) "select(cond, B, C)" should |
| 772 | // have been simplified to the common value of B and C already. Analysing |
| 773 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 774 | // for threading over phi nodes. |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 779 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 780 | const TargetData *TD, |
| 781 | const TargetLibraryInfo *TLI, |
| 782 | const DominatorTree *DT) { |
| 783 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, TD, TLI, DT, RecursionLimit); |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 786 | /// SimplifyMulInst - Given operands for a Mul, see if we can |
| 787 | /// fold the result. If not, this returns null. |
| 788 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 789 | const TargetLibraryInfo *TLI, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 790 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 791 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 792 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 793 | Constant *Ops[] = { CLHS, CRHS }; |
| 794 | return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 795 | Ops, TD, TLI); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | // Canonicalize the constant to the RHS. |
| 799 | std::swap(Op0, Op1); |
| 800 | } |
| 801 | |
| 802 | // X * undef -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 803 | if (match(Op1, m_Undef())) |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 804 | return Constant::getNullValue(Op0->getType()); |
| 805 | |
| 806 | // X * 0 -> 0 |
| 807 | if (match(Op1, m_Zero())) |
| 808 | return Op1; |
| 809 | |
| 810 | // X * 1 -> X |
| 811 | if (match(Op1, m_One())) |
| 812 | return Op0; |
| 813 | |
Duncan Sands | 1895e98 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 814 | // (X / Y) * Y -> X if the division is exact. |
Benjamin Kramer | 55c6d57 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 815 | Value *X = 0; |
| 816 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 817 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 818 | return X; |
Duncan Sands | 1895e98 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 819 | |
Nick Lewycky | 5413880 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 820 | // i1 mul -> and. |
Duncan Sands | 75d289e | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 821 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 822 | if (Value *V = SimplifyAndInst(Op0, Op1, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 07f30fb | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 823 | return V; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 824 | |
| 825 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 826 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 827 | MaxRecurse)) |
| 828 | return V; |
| 829 | |
| 830 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 831 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 832 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 833 | return V; |
| 834 | |
| 835 | // If the operation is with the result of a select instruction, check whether |
| 836 | // operating on either branch of the select always yields the same value. |
| 837 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 838 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 839 | MaxRecurse)) |
| 840 | return V; |
| 841 | |
| 842 | // If the operation is with the result of a phi instruction, check whether |
| 843 | // operating on all incoming values of the phi always yields the same value. |
| 844 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 845 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 846 | MaxRecurse)) |
| 847 | return V; |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 853 | const TargetLibraryInfo *TLI, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 854 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 855 | return ::SimplifyMulInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 858 | /// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can |
| 859 | /// fold the result. If not, this returns null. |
Anders Carlsson | 479b4b9 | 2011-02-05 18:33:43 +0000 | [diff] [blame] | 860 | static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 861 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 862 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 863 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 864 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 865 | Constant *Ops[] = { C0, C1 }; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 866 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, TD, TLI); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 870 | bool isSigned = Opcode == Instruction::SDiv; |
| 871 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 872 | // X / undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 873 | if (match(Op1, m_Undef())) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 874 | return Op1; |
| 875 | |
| 876 | // undef / X -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 877 | if (match(Op0, m_Undef())) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 878 | return Constant::getNullValue(Op0->getType()); |
| 879 | |
| 880 | // 0 / X -> 0, we don't need to preserve faults! |
| 881 | if (match(Op0, m_Zero())) |
| 882 | return Op0; |
| 883 | |
| 884 | // X / 1 -> X |
| 885 | if (match(Op1, m_One())) |
| 886 | return Op0; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 887 | |
| 888 | if (Op0->getType()->isIntegerTy(1)) |
| 889 | // It can't be division by zero, hence it must be division by one. |
| 890 | return Op0; |
| 891 | |
| 892 | // X / X -> 1 |
| 893 | if (Op0 == Op1) |
| 894 | return ConstantInt::get(Op0->getType(), 1); |
| 895 | |
| 896 | // (X * Y) / Y -> X if the multiplication does not overflow. |
| 897 | Value *X = 0, *Y = 0; |
| 898 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 899 | if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1 |
Duncan Sands | 32a43cc | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 900 | OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); |
Duncan Sands | 4b72071 | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 901 | // If the Mul knows it does not overflow, then we are good to go. |
| 902 | if ((isSigned && Mul->hasNoSignedWrap()) || |
| 903 | (!isSigned && Mul->hasNoUnsignedWrap())) |
| 904 | return X; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 905 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 906 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 907 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 908 | return X; |
| 909 | } |
| 910 | |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 911 | // (X rem Y) / Y -> 0 |
| 912 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 913 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 914 | return Constant::getNullValue(Op0->getType()); |
| 915 | |
| 916 | // If the operation is with the result of a select instruction, check whether |
| 917 | // operating on either branch of the select always yields the same value. |
| 918 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 919 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, TLI, DT, |
| 920 | MaxRecurse)) |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 921 | return V; |
| 922 | |
| 923 | // If the operation is with the result of a phi instruction, check whether |
| 924 | // operating on all incoming values of the phi always yields the same value. |
| 925 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 926 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, TLI, DT, |
| 927 | MaxRecurse)) |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 928 | return V; |
| 929 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | /// SimplifySDivInst - Given operands for an SDiv, see if we can |
| 934 | /// fold the result. If not, this returns null. |
| 935 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 936 | const TargetLibraryInfo *TLI, |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 937 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 938 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, TD, TLI, DT, |
| 939 | MaxRecurse)) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 940 | return V; |
| 941 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 946 | const TargetLibraryInfo *TLI, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 947 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 948 | return ::SimplifySDivInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /// SimplifyUDivInst - Given operands for a UDiv, see if we can |
| 952 | /// fold the result. If not, this returns null. |
| 953 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 954 | const TargetLibraryInfo *TLI, |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 955 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 956 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, TD, TLI, DT, |
| 957 | MaxRecurse)) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 958 | return V; |
| 959 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 964 | const TargetLibraryInfo *TLI, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 965 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 966 | return ::SimplifyUDivInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 969 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 970 | const TargetLibraryInfo *, |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 971 | const DominatorTree *, unsigned) { |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 972 | // undef / X -> undef (the undef could be a snan). |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 973 | if (match(Op0, m_Undef())) |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 974 | return Op0; |
| 975 | |
| 976 | // X / undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 977 | if (match(Op1, m_Undef())) |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 978 | return Op1; |
| 979 | |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 984 | const TargetLibraryInfo *TLI, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 985 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 986 | return ::SimplifyFDivInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 989 | /// SimplifyRem - Given operands for an SRem or URem, see if we can |
| 990 | /// fold the result. If not, this returns null. |
| 991 | static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 992 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 993 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 994 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 995 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 996 | Constant *Ops[] = { C0, C1 }; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 997 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, TD, TLI); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1001 | // X % undef -> undef |
| 1002 | if (match(Op1, m_Undef())) |
| 1003 | return Op1; |
| 1004 | |
| 1005 | // undef % X -> 0 |
| 1006 | if (match(Op0, m_Undef())) |
| 1007 | return Constant::getNullValue(Op0->getType()); |
| 1008 | |
| 1009 | // 0 % X -> 0, we don't need to preserve faults! |
| 1010 | if (match(Op0, m_Zero())) |
| 1011 | return Op0; |
| 1012 | |
| 1013 | // X % 0 -> undef, we don't need to preserve faults! |
| 1014 | if (match(Op1, m_Zero())) |
| 1015 | return UndefValue::get(Op0->getType()); |
| 1016 | |
| 1017 | // X % 1 -> 0 |
| 1018 | if (match(Op1, m_One())) |
| 1019 | return Constant::getNullValue(Op0->getType()); |
| 1020 | |
| 1021 | if (Op0->getType()->isIntegerTy(1)) |
| 1022 | // It can't be remainder by zero, hence it must be remainder by one. |
| 1023 | return Constant::getNullValue(Op0->getType()); |
| 1024 | |
| 1025 | // X % X -> 0 |
| 1026 | if (Op0 == Op1) |
| 1027 | return Constant::getNullValue(Op0->getType()); |
| 1028 | |
| 1029 | // If the operation is with the result of a select instruction, check whether |
| 1030 | // operating on either branch of the select always yields the same value. |
| 1031 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1032 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1033 | return V; |
| 1034 | |
| 1035 | // If the operation is with the result of a phi instruction, check whether |
| 1036 | // operating on all incoming values of the phi always yields the same value. |
| 1037 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1038 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1039 | return V; |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | /// SimplifySRemInst - Given operands for an SRem, see if we can |
| 1045 | /// fold the result. If not, this returns null. |
| 1046 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1047 | const TargetLibraryInfo *TLI, |
| 1048 | const DominatorTree *DT, |
| 1049 | unsigned MaxRecurse) { |
| 1050 | if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1051 | return V; |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1057 | const TargetLibraryInfo *TLI, |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1058 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1059 | return ::SimplifySRemInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /// SimplifyURemInst - Given operands for a URem, see if we can |
| 1063 | /// fold the result. If not, this returns null. |
| 1064 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1065 | const TargetLibraryInfo *TLI, |
| 1066 | const DominatorTree *DT, |
| 1067 | unsigned MaxRecurse) { |
| 1068 | if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1069 | return V; |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1075 | const TargetLibraryInfo *TLI, |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1076 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1077 | return ::SimplifyURemInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1081 | const TargetLibraryInfo *, |
| 1082 | const DominatorTree *, |
| 1083 | unsigned) { |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1084 | // undef % X -> undef (the undef could be a snan). |
| 1085 | if (match(Op0, m_Undef())) |
| 1086 | return Op0; |
| 1087 | |
| 1088 | // X % undef -> undef |
| 1089 | if (match(Op1, m_Undef())) |
| 1090 | return Op1; |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1096 | const TargetLibraryInfo *TLI, |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1097 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1098 | return ::SimplifyFRemInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1101 | /// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1102 | /// fold the result. If not, this returns null. |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1103 | static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1104 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 1105 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1106 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1107 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1108 | Constant *Ops[] = { C0, C1 }; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1109 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, TD, TLI); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1113 | // 0 shift by X -> 0 |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1114 | if (match(Op0, m_Zero())) |
| 1115 | return Op0; |
| 1116 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1117 | // X shift by 0 -> X |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1118 | if (match(Op1, m_Zero())) |
| 1119 | return Op0; |
| 1120 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1121 | // X shift by undef -> undef because it may shift by the bitwidth. |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1122 | if (match(Op1, m_Undef())) |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1123 | return Op1; |
| 1124 | |
| 1125 | // Shifting by the bitwidth or more is undefined. |
| 1126 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) |
| 1127 | if (CI->getValue().getLimitedValue() >= |
| 1128 | Op0->getType()->getScalarSizeInBits()) |
| 1129 | return UndefValue::get(Op0->getType()); |
| 1130 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1131 | // If the operation is with the result of a select instruction, check whether |
| 1132 | // operating on either branch of the select always yields the same value. |
| 1133 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1134 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1135 | return V; |
| 1136 | |
| 1137 | // If the operation is with the result of a phi instruction, check whether |
| 1138 | // operating on all incoming values of the phi always yields the same value. |
| 1139 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1140 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1141 | return V; |
| 1142 | |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | /// SimplifyShlInst - Given operands for an Shl, see if we can |
| 1147 | /// fold the result. If not, this returns null. |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1148 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1149 | const TargetData *TD, |
| 1150 | const TargetLibraryInfo *TLI, |
| 1151 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 1152 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1153 | return V; |
| 1154 | |
| 1155 | // undef << X -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1156 | if (match(Op0, m_Undef())) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1157 | return Constant::getNullValue(Op0->getType()); |
| 1158 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1159 | // (X >> A) << A -> X |
| 1160 | Value *X; |
Benjamin Kramer | 55c6d57 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1161 | if (match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1))))) |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1162 | return X; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1163 | return 0; |
| 1164 | } |
| 1165 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1166 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1167 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 1168 | const DominatorTree *DT) { |
| 1169 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, TD, TLI, DT, RecursionLimit); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /// SimplifyLShrInst - Given operands for an LShr, see if we can |
| 1173 | /// fold the result. If not, this returns null. |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1174 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1175 | const TargetData *TD, |
| 1176 | const TargetLibraryInfo *TLI, |
| 1177 | const DominatorTree *DT, |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1178 | unsigned MaxRecurse) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1179 | if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1180 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1181 | |
| 1182 | // undef >>l X -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1183 | if (match(Op0, m_Undef())) |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1184 | return Constant::getNullValue(Op0->getType()); |
| 1185 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1186 | // (X << A) >> A -> X |
| 1187 | Value *X; |
| 1188 | if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) && |
| 1189 | cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap()) |
| 1190 | return X; |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1191 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1192 | return 0; |
| 1193 | } |
| 1194 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1195 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1196 | const TargetData *TD, |
| 1197 | const TargetLibraryInfo *TLI, |
| 1198 | const DominatorTree *DT) { |
| 1199 | return ::SimplifyLShrInst(Op0, Op1, isExact, TD, TLI, DT, RecursionLimit); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /// SimplifyAShrInst - Given operands for an AShr, see if we can |
| 1203 | /// fold the result. If not, this returns null. |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1204 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1205 | const TargetData *TD, |
| 1206 | const TargetLibraryInfo *TLI, |
| 1207 | const DominatorTree *DT, |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1208 | unsigned MaxRecurse) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1209 | if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1210 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1211 | |
| 1212 | // all ones >>a X -> all ones |
| 1213 | if (match(Op0, m_AllOnes())) |
| 1214 | return Op0; |
| 1215 | |
| 1216 | // undef >>a X -> all ones |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1217 | if (match(Op0, m_Undef())) |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1218 | return Constant::getAllOnesValue(Op0->getType()); |
| 1219 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1220 | // (X << A) >> A -> X |
| 1221 | Value *X; |
| 1222 | if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) && |
| 1223 | cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap()) |
| 1224 | return X; |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1225 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1226 | return 0; |
| 1227 | } |
| 1228 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1229 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1230 | const TargetData *TD, |
| 1231 | const TargetLibraryInfo *TLI, |
| 1232 | const DominatorTree *DT) { |
| 1233 | return ::SimplifyAShrInst(Op0, Op1, isExact, TD, TLI, DT, RecursionLimit); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1236 | /// SimplifyAndInst - Given operands for an And, see if we can |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1237 | /// fold the result. If not, this returns null. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1238 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1239 | const TargetLibraryInfo *TLI, |
| 1240 | const DominatorTree *DT, |
| 1241 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1242 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1243 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1244 | Constant *Ops[] = { CLHS, CRHS }; |
| 1245 | return ConstantFoldInstOperands(Instruction::And, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1246 | Ops, TD, TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1247 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1248 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1249 | // Canonicalize the constant to the RHS. |
| 1250 | std::swap(Op0, Op1); |
| 1251 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1253 | // X & undef -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1254 | if (match(Op1, m_Undef())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1255 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1257 | // X & X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1258 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1259 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1260 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1261 | // X & 0 = 0 |
| 1262 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1263 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1264 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1265 | // X & -1 = X |
| 1266 | if (match(Op1, m_AllOnes())) |
| 1267 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1269 | // A & ~A = ~A & A = 0 |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1270 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1271 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1272 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1273 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1274 | // (A | ?) & A = A |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1275 | Value *A = 0, *B = 0; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1276 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1277 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1278 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1279 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1280 | // A & (A | ?) = A |
| 1281 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1282 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1283 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1284 | |
Duncan Sands | dd3149d | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1285 | // A & (-A) = A if A is a power of two or zero. |
| 1286 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1287 | match(Op1, m_Neg(m_Specific(Op0)))) { |
| 1288 | if (isPowerOfTwo(Op0, TD, /*OrZero*/true)) |
| 1289 | return Op0; |
| 1290 | if (isPowerOfTwo(Op1, TD, /*OrZero*/true)) |
| 1291 | return Op1; |
| 1292 | } |
| 1293 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1294 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1295 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, TD, TLI, |
| 1296 | DT, MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1297 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1298 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1299 | // And distributes over Or. Try some generic simplifications based on this. |
| 1300 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1301 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1302 | return V; |
| 1303 | |
| 1304 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1305 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1306 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1307 | return V; |
| 1308 | |
| 1309 | // Or distributes over And. Try some generic simplifications based on this. |
| 1310 | if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1311 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1312 | return V; |
| 1313 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1314 | // If the operation is with the result of a select instruction, check whether |
| 1315 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1316 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1317 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD, TLI, |
| 1318 | DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1319 | return V; |
| 1320 | |
| 1321 | // If the operation is with the result of a phi instruction, check whether |
| 1322 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1323 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1324 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1325 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1326 | return V; |
| 1327 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1331 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1332 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1333 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1334 | return ::SimplifyAndInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1337 | /// SimplifyOrInst - Given operands for an Or, see if we can |
| 1338 | /// fold the result. If not, this returns null. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1339 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1340 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1341 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1342 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1343 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1344 | Constant *Ops[] = { CLHS, CRHS }; |
| 1345 | return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1346 | Ops, TD, TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1347 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1348 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1349 | // Canonicalize the constant to the RHS. |
| 1350 | std::swap(Op0, Op1); |
| 1351 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1352 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1353 | // X | undef -> -1 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1354 | if (match(Op1, m_Undef())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1355 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1356 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1357 | // X | X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1358 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1359 | return Op0; |
| 1360 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1361 | // X | 0 = X |
| 1362 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1363 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1364 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1365 | // X | -1 = -1 |
| 1366 | if (match(Op1, m_AllOnes())) |
| 1367 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1368 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1369 | // A | ~A = ~A | A = -1 |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1370 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1371 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1372 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1374 | // (A & ?) | A = A |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1375 | Value *A = 0, *B = 0; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1376 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1377 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1378 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1380 | // A | (A & ?) = A |
| 1381 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1382 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1383 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1384 | |
Benjamin Kramer | 38f7f66 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1385 | // ~(A & ?) | A = -1 |
| 1386 | if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1387 | (A == Op1 || B == Op1)) |
| 1388 | return Constant::getAllOnesValue(Op1->getType()); |
| 1389 | |
| 1390 | // A | ~(A & ?) = -1 |
| 1391 | if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1392 | (A == Op0 || B == Op0)) |
| 1393 | return Constant::getAllOnesValue(Op0->getType()); |
| 1394 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1395 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1396 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, TD, TLI, |
| 1397 | DT, MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1398 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1399 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1400 | // Or distributes over And. Try some generic simplifications based on this. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1401 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, TD, |
| 1402 | TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1403 | return V; |
| 1404 | |
| 1405 | // And distributes over Or. Try some generic simplifications based on this. |
| 1406 | if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1407 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1408 | return V; |
| 1409 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1410 | // If the operation is with the result of a select instruction, check whether |
| 1411 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1412 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1413 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1414 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1415 | return V; |
| 1416 | |
| 1417 | // If the operation is with the result of a phi instruction, check whether |
| 1418 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1419 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1420 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1421 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1422 | return V; |
| 1423 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1424 | return 0; |
| 1425 | } |
| 1426 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1427 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1428 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1429 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1430 | return ::SimplifyOrInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1431 | } |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1432 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1433 | /// SimplifyXorInst - Given operands for a Xor, see if we can |
| 1434 | /// fold the result. If not, this returns null. |
| 1435 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1436 | const TargetLibraryInfo *TLI, |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1437 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 1438 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1439 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1440 | Constant *Ops[] = { CLHS, CRHS }; |
| 1441 | return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1442 | Ops, TD, TLI); |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | // Canonicalize the constant to the RHS. |
| 1446 | std::swap(Op0, Op1); |
| 1447 | } |
| 1448 | |
| 1449 | // A ^ undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1450 | if (match(Op1, m_Undef())) |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1451 | return Op1; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1452 | |
| 1453 | // A ^ 0 = A |
| 1454 | if (match(Op1, m_Zero())) |
| 1455 | return Op0; |
| 1456 | |
Eli Friedman | f23d4ad | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 1457 | // A ^ A = 0 |
| 1458 | if (Op0 == Op1) |
| 1459 | return Constant::getNullValue(Op0->getType()); |
| 1460 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1461 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1462 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1463 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1464 | return Constant::getAllOnesValue(Op0->getType()); |
| 1465 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1466 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1467 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, TD, TLI, |
| 1468 | DT, MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1469 | return V; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1470 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1471 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1472 | if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1473 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1474 | return V; |
| 1475 | |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1476 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1477 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1478 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1479 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1480 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1481 | // have been simplified to the common value of B and C already. Analysing |
| 1482 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1483 | // for threading over phi nodes. |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1484 | |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1489 | const TargetLibraryInfo *TLI, |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1490 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1491 | return ::SimplifyXorInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1494 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1495 | return CmpInst::makeCmpResultType(Op->getType()); |
| 1496 | } |
| 1497 | |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1498 | /// ExtractEquivalentCondition - Rummage around inside V looking for something |
| 1499 | /// equivalent to the comparison "LHS Pred RHS". Return such a value if found, |
| 1500 | /// otherwise return null. Helper function for analyzing max/min idioms. |
| 1501 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 1502 | Value *LHS, Value *RHS) { |
| 1503 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 1504 | if (!SI) |
| 1505 | return 0; |
| 1506 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1507 | if (!Cmp) |
| 1508 | return 0; |
| 1509 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 1510 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 1511 | return Cmp; |
| 1512 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 1513 | LHS == CmpRHS && RHS == CmpLHS) |
| 1514 | return Cmp; |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1518 | /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can |
| 1519 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1520 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1521 | const TargetData *TD, |
| 1522 | const TargetLibraryInfo *TLI, |
| 1523 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1524 | unsigned MaxRecurse) { |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1525 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1526 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1528 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 1529 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1530 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD, TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1531 | |
| 1532 | // If we have a constant, make sure it is on the RHS. |
| 1533 | std::swap(LHS, RHS); |
| 1534 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 1535 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1536 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1537 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 1538 | Type *OpTy = LHS->getType(); // The operand type. |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1539 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1540 | // icmp X, X -> true/false |
Chris Lattner | c8e14b3 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 1541 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 1542 | // because X could be 0. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1543 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1544 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1545 | |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1546 | // Special case logic when the operands have i1 type. |
Nick Lewycky | 66d004e | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 1547 | if (OpTy->getScalarType()->isIntegerTy(1)) { |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1548 | switch (Pred) { |
| 1549 | default: break; |
| 1550 | case ICmpInst::ICMP_EQ: |
| 1551 | // X == 1 -> X |
| 1552 | if (match(RHS, m_One())) |
| 1553 | return LHS; |
| 1554 | break; |
| 1555 | case ICmpInst::ICMP_NE: |
| 1556 | // X != 0 -> X |
| 1557 | if (match(RHS, m_Zero())) |
| 1558 | return LHS; |
| 1559 | break; |
| 1560 | case ICmpInst::ICMP_UGT: |
| 1561 | // X >u 0 -> X |
| 1562 | if (match(RHS, m_Zero())) |
| 1563 | return LHS; |
| 1564 | break; |
| 1565 | case ICmpInst::ICMP_UGE: |
| 1566 | // X >=u 1 -> X |
| 1567 | if (match(RHS, m_One())) |
| 1568 | return LHS; |
| 1569 | break; |
| 1570 | case ICmpInst::ICMP_SLT: |
| 1571 | // X <s 0 -> X |
| 1572 | if (match(RHS, m_Zero())) |
| 1573 | return LHS; |
| 1574 | break; |
| 1575 | case ICmpInst::ICMP_SLE: |
| 1576 | // X <=s -1 -> X |
| 1577 | if (match(RHS, m_One())) |
| 1578 | return LHS; |
| 1579 | break; |
| 1580 | } |
| 1581 | } |
| 1582 | |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1583 | // icmp <alloca*>, <global/alloca*/null> - Different stack variables have |
| 1584 | // different addresses, and what's more the address of a stack variable is |
| 1585 | // never null or equal to the address of a global. Note that generalizing |
| 1586 | // to the case where LHS is a global variable address or null is pointless, |
| 1587 | // since if both LHS and RHS are constants then we already constant folded |
| 1588 | // the compare, and if only one of them is then we moved it to RHS already. |
| 1589 | if (isa<AllocaInst>(LHS) && (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) || |
| 1590 | isa<ConstantPointerNull>(RHS))) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 1591 | // We already know that LHS != RHS. |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1592 | return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); |
| 1593 | |
| 1594 | // If we are comparing with zero then try hard since this is a common case. |
| 1595 | if (match(RHS, m_Zero())) { |
| 1596 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 1597 | switch (Pred) { |
| 1598 | default: |
| 1599 | assert(false && "Unknown ICmp predicate!"); |
| 1600 | case ICmpInst::ICMP_ULT: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1601 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1602 | case ICmpInst::ICMP_UGE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1603 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1604 | case ICmpInst::ICMP_EQ: |
| 1605 | case ICmpInst::ICMP_ULE: |
| 1606 | if (isKnownNonZero(LHS, TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1607 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1608 | break; |
| 1609 | case ICmpInst::ICMP_NE: |
| 1610 | case ICmpInst::ICMP_UGT: |
| 1611 | if (isKnownNonZero(LHS, TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1612 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1613 | break; |
| 1614 | case ICmpInst::ICMP_SLT: |
| 1615 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1616 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1617 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1618 | if (LHSKnownNonNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1619 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1620 | break; |
| 1621 | case ICmpInst::ICMP_SLE: |
| 1622 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1623 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1624 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1625 | if (LHSKnownNonNegative && isKnownNonZero(LHS, TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1626 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1627 | break; |
| 1628 | case ICmpInst::ICMP_SGE: |
| 1629 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1630 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1631 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1632 | if (LHSKnownNonNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1633 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1634 | break; |
| 1635 | case ICmpInst::ICMP_SGT: |
| 1636 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1637 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1638 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1639 | if (LHSKnownNonNegative && isKnownNonZero(LHS, TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1640 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1641 | break; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | // See if we are doing a comparison with a constant integer. |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1646 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1647 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
| 1648 | ConstantRange RHS_CR = ICmpInst::makeConstantRange(Pred, CI->getValue()); |
| 1649 | if (RHS_CR.isEmptySet()) |
| 1650 | return ConstantInt::getFalse(CI->getContext()); |
| 1651 | if (RHS_CR.isFullSet()) |
| 1652 | return ConstantInt::getTrue(CI->getContext()); |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1653 | |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1654 | // Many binary operators with constant RHS have easy to compute constant |
| 1655 | // range. Use them to check whether the comparison is a tautology. |
| 1656 | uint32_t Width = CI->getBitWidth(); |
| 1657 | APInt Lower = APInt(Width, 0); |
| 1658 | APInt Upper = APInt(Width, 0); |
| 1659 | ConstantInt *CI2; |
| 1660 | if (match(LHS, m_URem(m_Value(), m_ConstantInt(CI2)))) { |
| 1661 | // 'urem x, CI2' produces [0, CI2). |
| 1662 | Upper = CI2->getValue(); |
| 1663 | } else if (match(LHS, m_SRem(m_Value(), m_ConstantInt(CI2)))) { |
| 1664 | // 'srem x, CI2' produces (-|CI2|, |CI2|). |
| 1665 | Upper = CI2->getValue().abs(); |
| 1666 | Lower = (-Upper) + 1; |
Duncan Sands | c65c747 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 1667 | } else if (match(LHS, m_UDiv(m_ConstantInt(CI2), m_Value()))) { |
| 1668 | // 'udiv CI2, x' produces [0, CI2]. |
Eli Friedman | 7781ae5 | 2011-11-08 21:08:02 +0000 | [diff] [blame] | 1669 | Upper = CI2->getValue() + 1; |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1670 | } else if (match(LHS, m_UDiv(m_Value(), m_ConstantInt(CI2)))) { |
| 1671 | // 'udiv x, CI2' produces [0, UINT_MAX / CI2]. |
| 1672 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 1673 | if (!CI2->isZero()) |
| 1674 | Upper = NegOne.udiv(CI2->getValue()) + 1; |
| 1675 | } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) { |
| 1676 | // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2]. |
| 1677 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 1678 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 1679 | APInt Val = CI2->getValue().abs(); |
| 1680 | if (!Val.isMinValue()) { |
| 1681 | Lower = IntMin.sdiv(Val); |
| 1682 | Upper = IntMax.sdiv(Val) + 1; |
| 1683 | } |
| 1684 | } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) { |
| 1685 | // 'lshr x, CI2' produces [0, UINT_MAX >> CI2]. |
| 1686 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 1687 | if (CI2->getValue().ult(Width)) |
| 1688 | Upper = NegOne.lshr(CI2->getValue()) + 1; |
| 1689 | } else if (match(LHS, m_AShr(m_Value(), m_ConstantInt(CI2)))) { |
| 1690 | // 'ashr x, CI2' produces [INT_MIN >> CI2, INT_MAX >> CI2]. |
| 1691 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 1692 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 1693 | if (CI2->getValue().ult(Width)) { |
| 1694 | Lower = IntMin.ashr(CI2->getValue()); |
| 1695 | Upper = IntMax.ashr(CI2->getValue()) + 1; |
| 1696 | } |
| 1697 | } else if (match(LHS, m_Or(m_Value(), m_ConstantInt(CI2)))) { |
| 1698 | // 'or x, CI2' produces [CI2, UINT_MAX]. |
| 1699 | Lower = CI2->getValue(); |
| 1700 | } else if (match(LHS, m_And(m_Value(), m_ConstantInt(CI2)))) { |
| 1701 | // 'and x, CI2' produces [0, CI2]. |
| 1702 | Upper = CI2->getValue() + 1; |
| 1703 | } |
| 1704 | if (Lower != Upper) { |
| 1705 | ConstantRange LHS_CR = ConstantRange(Lower, Upper); |
| 1706 | if (RHS_CR.contains(LHS_CR)) |
| 1707 | return ConstantInt::getTrue(RHS->getContext()); |
| 1708 | if (RHS_CR.inverse().contains(LHS_CR)) |
| 1709 | return ConstantInt::getFalse(RHS->getContext()); |
| 1710 | } |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1713 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 1714 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 1715 | Instruction *LI = cast<CastInst>(LHS); |
| 1716 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1717 | Type *SrcTy = SrcOp->getType(); |
| 1718 | Type *DstTy = LI->getType(); |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1719 | |
| 1720 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 1721 | // if the integer type is the same size as the pointer type. |
| 1722 | if (MaxRecurse && TD && isa<PtrToIntInst>(LI) && |
| 1723 | TD->getPointerSizeInBits() == DstTy->getPrimitiveSizeInBits()) { |
| 1724 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 1725 | // Transfer the cast to the constant. |
| 1726 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 1727 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1728 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1729 | return V; |
| 1730 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 1731 | if (RI->getOperand(0)->getType() == SrcTy) |
| 1732 | // Compare without the cast. |
| 1733 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1734 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1735 | return V; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | if (isa<ZExtInst>(LHS)) { |
| 1740 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 1741 | // same type. |
| 1742 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 1743 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 1744 | // Compare X and Y. Note that signed predicates become unsigned. |
| 1745 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1746 | SrcOp, RI->getOperand(0), TD, TLI, DT, |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1747 | MaxRecurse-1)) |
| 1748 | return V; |
| 1749 | } |
| 1750 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 1751 | // too. If not, then try to deduce the result of the comparison. |
| 1752 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1753 | // Compute the constant that would happen if we truncated to SrcTy then |
| 1754 | // reextended to DstTy. |
| 1755 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 1756 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 1757 | |
| 1758 | // If the re-extended constant didn't change then this is effectively |
| 1759 | // also a case of comparing two zero-extended values. |
| 1760 | if (RExt == CI && MaxRecurse) |
| 1761 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1762 | SrcOp, Trunc, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1763 | return V; |
| 1764 | |
| 1765 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 1766 | // there. Use this to work out the result of the comparison. |
| 1767 | if (RExt != CI) { |
| 1768 | switch (Pred) { |
| 1769 | default: |
| 1770 | assert(false && "Unknown ICmp predicate!"); |
| 1771 | // LHS <u RHS. |
| 1772 | case ICmpInst::ICMP_EQ: |
| 1773 | case ICmpInst::ICMP_UGT: |
| 1774 | case ICmpInst::ICMP_UGE: |
| 1775 | return ConstantInt::getFalse(CI->getContext()); |
| 1776 | |
| 1777 | case ICmpInst::ICMP_NE: |
| 1778 | case ICmpInst::ICMP_ULT: |
| 1779 | case ICmpInst::ICMP_ULE: |
| 1780 | return ConstantInt::getTrue(CI->getContext()); |
| 1781 | |
| 1782 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 1783 | // is non-negative then LHS <s RHS. |
| 1784 | case ICmpInst::ICMP_SGT: |
| 1785 | case ICmpInst::ICMP_SGE: |
| 1786 | return CI->getValue().isNegative() ? |
| 1787 | ConstantInt::getTrue(CI->getContext()) : |
| 1788 | ConstantInt::getFalse(CI->getContext()); |
| 1789 | |
| 1790 | case ICmpInst::ICMP_SLT: |
| 1791 | case ICmpInst::ICMP_SLE: |
| 1792 | return CI->getValue().isNegative() ? |
| 1793 | ConstantInt::getFalse(CI->getContext()) : |
| 1794 | ConstantInt::getTrue(CI->getContext()); |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | if (isa<SExtInst>(LHS)) { |
| 1801 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 1802 | // same type. |
| 1803 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 1804 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 1805 | // Compare X and Y. Note that the predicate does not change. |
| 1806 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1807 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1808 | return V; |
| 1809 | } |
| 1810 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 1811 | // too. If not, then try to deduce the result of the comparison. |
| 1812 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1813 | // Compute the constant that would happen if we truncated to SrcTy then |
| 1814 | // reextended to DstTy. |
| 1815 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 1816 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 1817 | |
| 1818 | // If the re-extended constant didn't change then this is effectively |
| 1819 | // also a case of comparing two sign-extended values. |
| 1820 | if (RExt == CI && MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1821 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, TD, TLI, DT, |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1822 | MaxRecurse-1)) |
| 1823 | return V; |
| 1824 | |
| 1825 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 1826 | // bits there. Use this to work out the result of the comparison. |
| 1827 | if (RExt != CI) { |
| 1828 | switch (Pred) { |
| 1829 | default: |
| 1830 | assert(false && "Unknown ICmp predicate!"); |
| 1831 | case ICmpInst::ICMP_EQ: |
| 1832 | return ConstantInt::getFalse(CI->getContext()); |
| 1833 | case ICmpInst::ICMP_NE: |
| 1834 | return ConstantInt::getTrue(CI->getContext()); |
| 1835 | |
| 1836 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 1837 | // LHS >s RHS. |
| 1838 | case ICmpInst::ICMP_SGT: |
| 1839 | case ICmpInst::ICMP_SGE: |
| 1840 | return CI->getValue().isNegative() ? |
| 1841 | ConstantInt::getTrue(CI->getContext()) : |
| 1842 | ConstantInt::getFalse(CI->getContext()); |
| 1843 | case ICmpInst::ICMP_SLT: |
| 1844 | case ICmpInst::ICMP_SLE: |
| 1845 | return CI->getValue().isNegative() ? |
| 1846 | ConstantInt::getFalse(CI->getContext()) : |
| 1847 | ConstantInt::getTrue(CI->getContext()); |
| 1848 | |
| 1849 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 1850 | // LHS >u RHS. |
| 1851 | case ICmpInst::ICMP_UGT: |
| 1852 | case ICmpInst::ICMP_UGE: |
| 1853 | // Comparison is true iff the LHS <s 0. |
| 1854 | if (MaxRecurse) |
| 1855 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 1856 | Constant::getNullValue(SrcTy), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1857 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1858 | return V; |
| 1859 | break; |
| 1860 | case ICmpInst::ICMP_ULT: |
| 1861 | case ICmpInst::ICMP_ULE: |
| 1862 | // Comparison is true iff the LHS >=s 0. |
| 1863 | if (MaxRecurse) |
| 1864 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 1865 | Constant::getNullValue(SrcTy), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1866 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1867 | return V; |
| 1868 | break; |
| 1869 | } |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1875 | // Special logic for binary operators. |
| 1876 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 1877 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 1878 | if (MaxRecurse && (LBO || RBO)) { |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1879 | // Analyze the case when either LHS or RHS is an add instruction. |
| 1880 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 1881 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 1882 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 1883 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 1884 | A = LBO->getOperand(0); B = LBO->getOperand(1); |
| 1885 | NoLHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 1886 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 1887 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 1888 | } |
| 1889 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 1890 | C = RBO->getOperand(0); D = RBO->getOperand(1); |
| 1891 | NoRHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 1892 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 1893 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 1894 | } |
| 1895 | |
| 1896 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 1897 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 1898 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 1899 | Constant::getNullValue(RHS->getType()), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1900 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1901 | return V; |
| 1902 | |
| 1903 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 1904 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 1905 | if (Value *V = SimplifyICmpInst(Pred, |
| 1906 | Constant::getNullValue(LHS->getType()), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1907 | C == LHS ? D : C, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1908 | return V; |
| 1909 | |
| 1910 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 1911 | if (A && C && (A == C || A == D || B == C || B == D) && |
| 1912 | NoLHSWrapProblem && NoRHSWrapProblem) { |
| 1913 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
| 1914 | Value *Y = (A == C || A == D) ? B : A; |
| 1915 | Value *Z = (C == A || C == B) ? D : C; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1916 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1917 | return V; |
| 1918 | } |
| 1919 | } |
| 1920 | |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 1921 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 1922 | bool KnownNonNegative, KnownNegative; |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1923 | switch (Pred) { |
| 1924 | default: |
| 1925 | break; |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 1926 | case ICmpInst::ICMP_SGT: |
| 1927 | case ICmpInst::ICMP_SGE: |
| 1928 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, TD); |
| 1929 | if (!KnownNonNegative) |
| 1930 | break; |
| 1931 | // fall-through |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1932 | case ICmpInst::ICMP_EQ: |
| 1933 | case ICmpInst::ICMP_UGT: |
| 1934 | case ICmpInst::ICMP_UGE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1935 | return getFalse(ITy); |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 1936 | case ICmpInst::ICMP_SLT: |
| 1937 | case ICmpInst::ICMP_SLE: |
| 1938 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, TD); |
| 1939 | if (!KnownNonNegative) |
| 1940 | break; |
| 1941 | // fall-through |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1942 | case ICmpInst::ICMP_NE: |
| 1943 | case ICmpInst::ICMP_ULT: |
| 1944 | case ICmpInst::ICMP_ULE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1945 | return getTrue(ITy); |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1946 | } |
| 1947 | } |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 1948 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
| 1949 | bool KnownNonNegative, KnownNegative; |
| 1950 | switch (Pred) { |
| 1951 | default: |
| 1952 | break; |
| 1953 | case ICmpInst::ICMP_SGT: |
| 1954 | case ICmpInst::ICMP_SGE: |
| 1955 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, TD); |
| 1956 | if (!KnownNonNegative) |
| 1957 | break; |
| 1958 | // fall-through |
Nick Lewycky | a0e2f38 | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 1959 | case ICmpInst::ICMP_NE: |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 1960 | case ICmpInst::ICMP_UGT: |
| 1961 | case ICmpInst::ICMP_UGE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1962 | return getTrue(ITy); |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 1963 | case ICmpInst::ICMP_SLT: |
| 1964 | case ICmpInst::ICMP_SLE: |
| 1965 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, TD); |
| 1966 | if (!KnownNonNegative) |
| 1967 | break; |
| 1968 | // fall-through |
Nick Lewycky | a0e2f38 | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 1969 | case ICmpInst::ICMP_EQ: |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 1970 | case ICmpInst::ICMP_ULT: |
| 1971 | case ICmpInst::ICMP_ULE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1972 | return getFalse(ITy); |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 1973 | } |
| 1974 | } |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1975 | |
Duncan Sands | c65c747 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 1976 | // x udiv y <=u x. |
| 1977 | if (LBO && match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) { |
| 1978 | // icmp pred (X /u Y), X |
| 1979 | if (Pred == ICmpInst::ICMP_UGT) |
| 1980 | return getFalse(ITy); |
| 1981 | if (Pred == ICmpInst::ICMP_ULE) |
| 1982 | return getTrue(ITy); |
| 1983 | } |
| 1984 | |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 1985 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 1986 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 1987 | switch (LBO->getOpcode()) { |
| 1988 | default: break; |
| 1989 | case Instruction::UDiv: |
| 1990 | case Instruction::LShr: |
| 1991 | if (ICmpInst::isSigned(Pred)) |
| 1992 | break; |
| 1993 | // fall-through |
| 1994 | case Instruction::SDiv: |
| 1995 | case Instruction::AShr: |
Eli Friedman | b6e7cd6 | 2011-05-05 21:59:18 +0000 | [diff] [blame] | 1996 | if (!LBO->isExact() || !RBO->isExact()) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 1997 | break; |
| 1998 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1999 | RBO->getOperand(0), TD, TLI, DT, MaxRecurse-1)) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2000 | return V; |
| 2001 | break; |
| 2002 | case Instruction::Shl: { |
Duncan Sands | c9d904e | 2011-08-04 10:02:21 +0000 | [diff] [blame] | 2003 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2004 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2005 | if (!NUW && !NSW) |
| 2006 | break; |
| 2007 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2008 | break; |
| 2009 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2010 | RBO->getOperand(0), TD, TLI, DT, MaxRecurse-1)) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2011 | return V; |
| 2012 | break; |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2017 | // Simplify comparisons involving max/min. |
| 2018 | Value *A, *B; |
| 2019 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
| 2020 | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
| 2021 | |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2022 | // Signed variants on "max(a,b)>=a -> true". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2023 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2024 | if (A != RHS) std::swap(A, B); // smax(A, B) pred A. |
| 2025 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2026 | // We analyze this as smax(A, B) pred A. |
| 2027 | P = Pred; |
| 2028 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2029 | (A == LHS || B == LHS)) { |
| 2030 | if (A != LHS) std::swap(A, B); // A pred smax(A, B). |
| 2031 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2032 | // We analyze this as smax(A, B) swapped-pred A. |
| 2033 | P = CmpInst::getSwappedPredicate(Pred); |
| 2034 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2035 | (A == RHS || B == RHS)) { |
| 2036 | if (A != RHS) std::swap(A, B); // smin(A, B) pred A. |
| 2037 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2038 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2039 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2040 | P = CmpInst::getSwappedPredicate(Pred); |
| 2041 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2042 | (A == LHS || B == LHS)) { |
| 2043 | if (A != LHS) std::swap(A, B); // A pred smin(A, B). |
| 2044 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2045 | // We analyze this as smax(-A, -B) pred -A. |
| 2046 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2047 | P = Pred; |
| 2048 | } |
| 2049 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2050 | // Cases correspond to "max(A, B) p A". |
| 2051 | switch (P) { |
| 2052 | default: |
| 2053 | break; |
| 2054 | case CmpInst::ICMP_EQ: |
| 2055 | case CmpInst::ICMP_SLE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2056 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2057 | // in the max/min; if so, we can just return that. |
| 2058 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2059 | return V; |
| 2060 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2061 | return V; |
| 2062 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2063 | if (MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2064 | if (Value *V = SimplifyICmpInst(EqP, A, B, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2065 | return V; |
| 2066 | break; |
| 2067 | case CmpInst::ICMP_NE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2068 | case CmpInst::ICMP_SGT: { |
| 2069 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2070 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2071 | // tested in the max/min; if so, we can just return that. |
| 2072 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2073 | return V; |
| 2074 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2075 | return V; |
| 2076 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2077 | if (MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2078 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2079 | return V; |
| 2080 | break; |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2081 | } |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2082 | case CmpInst::ICMP_SGE: |
| 2083 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2084 | return getTrue(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2085 | case CmpInst::ICMP_SLT: |
| 2086 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2087 | return getFalse(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2091 | // Unsigned variants on "max(a,b)>=a -> true". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2092 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2093 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2094 | if (A != RHS) std::swap(A, B); // umax(A, B) pred A. |
| 2095 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2096 | // We analyze this as umax(A, B) pred A. |
| 2097 | P = Pred; |
| 2098 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2099 | (A == LHS || B == LHS)) { |
| 2100 | if (A != LHS) std::swap(A, B); // A pred umax(A, B). |
| 2101 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2102 | // We analyze this as umax(A, B) swapped-pred A. |
| 2103 | P = CmpInst::getSwappedPredicate(Pred); |
| 2104 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2105 | (A == RHS || B == RHS)) { |
| 2106 | if (A != RHS) std::swap(A, B); // umin(A, B) pred A. |
| 2107 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2108 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 2109 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2110 | P = CmpInst::getSwappedPredicate(Pred); |
| 2111 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2112 | (A == LHS || B == LHS)) { |
| 2113 | if (A != LHS) std::swap(A, B); // A pred umin(A, B). |
| 2114 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2115 | // We analyze this as umax(-A, -B) pred -A. |
| 2116 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2117 | P = Pred; |
| 2118 | } |
| 2119 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2120 | // Cases correspond to "max(A, B) p A". |
| 2121 | switch (P) { |
| 2122 | default: |
| 2123 | break; |
| 2124 | case CmpInst::ICMP_EQ: |
| 2125 | case CmpInst::ICMP_ULE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2126 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2127 | // in the max/min; if so, we can just return that. |
| 2128 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2129 | return V; |
| 2130 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2131 | return V; |
| 2132 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2133 | if (MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2134 | if (Value *V = SimplifyICmpInst(EqP, A, B, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2135 | return V; |
| 2136 | break; |
| 2137 | case CmpInst::ICMP_NE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2138 | case CmpInst::ICMP_UGT: { |
| 2139 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2140 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2141 | // tested in the max/min; if so, we can just return that. |
| 2142 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2143 | return V; |
| 2144 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2145 | return V; |
| 2146 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2147 | if (MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2148 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2149 | return V; |
| 2150 | break; |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2151 | } |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2152 | case CmpInst::ICMP_UGE: |
| 2153 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2154 | return getTrue(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2155 | case CmpInst::ICMP_ULT: |
| 2156 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2157 | return getFalse(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2161 | // Variants on "max(x,y) >= min(x,z)". |
| 2162 | Value *C, *D; |
| 2163 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2164 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 2165 | (A == C || A == D || B == C || B == D)) { |
| 2166 | // max(x, ?) pred min(x, ?). |
| 2167 | if (Pred == CmpInst::ICMP_SGE) |
| 2168 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2169 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2170 | if (Pred == CmpInst::ICMP_SLT) |
| 2171 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2172 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2173 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2174 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 2175 | (A == C || A == D || B == C || B == D)) { |
| 2176 | // min(x, ?) pred max(x, ?). |
| 2177 | if (Pred == CmpInst::ICMP_SLE) |
| 2178 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2179 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2180 | if (Pred == CmpInst::ICMP_SGT) |
| 2181 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2182 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2183 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2184 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 2185 | (A == C || A == D || B == C || B == D)) { |
| 2186 | // max(x, ?) pred min(x, ?). |
| 2187 | if (Pred == CmpInst::ICMP_UGE) |
| 2188 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2189 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2190 | if (Pred == CmpInst::ICMP_ULT) |
| 2191 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2192 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2193 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2194 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 2195 | (A == C || A == D || B == C || B == D)) { |
| 2196 | // min(x, ?) pred max(x, ?). |
| 2197 | if (Pred == CmpInst::ICMP_ULE) |
| 2198 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2199 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2200 | if (Pred == CmpInst::ICMP_UGT) |
| 2201 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2202 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2205 | // If the comparison is with the result of a select instruction, check whether |
| 2206 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2207 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2208 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2209 | return V; |
| 2210 | |
| 2211 | // If the comparison is with the result of a phi instruction, check whether |
| 2212 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2213 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2214 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 2215 | return V; |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2216 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 2217 | return 0; |
| 2218 | } |
| 2219 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2220 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2221 | const TargetData *TD, |
| 2222 | const TargetLibraryInfo *TLI, |
| 2223 | const DominatorTree *DT) { |
| 2224 | return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2227 | /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can |
| 2228 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2229 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2230 | const TargetData *TD, |
| 2231 | const TargetLibraryInfo *TLI, |
| 2232 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 2233 | unsigned MaxRecurse) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2234 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 2235 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 2236 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2237 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2238 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2239 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD, TLI); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2240 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2241 | // If we have a constant, make sure it is on the RHS. |
| 2242 | std::swap(LHS, RHS); |
| 2243 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 2244 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2245 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2246 | // Fold trivial predicates. |
| 2247 | if (Pred == FCmpInst::FCMP_FALSE) |
| 2248 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2249 | if (Pred == FCmpInst::FCMP_TRUE) |
| 2250 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2251 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2252 | if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef |
| 2253 | return UndefValue::get(GetCompareTy(LHS)); |
| 2254 | |
| 2255 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2256 | if (LHS == RHS) { |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2257 | if (CmpInst::isTrueWhenEqual(Pred)) |
| 2258 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2259 | if (CmpInst::isFalseWhenEqual(Pred)) |
| 2260 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2261 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2262 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2263 | // Handle fcmp with constant RHS |
| 2264 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2265 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 2266 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 2267 | if (CFP->getValueAPF().isNaN()) { |
| 2268 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
| 2269 | return ConstantInt::getFalse(CFP->getContext()); |
| 2270 | assert(FCmpInst::isUnordered(Pred) && |
| 2271 | "Comparison must be either ordered or unordered!"); |
| 2272 | // True if unordered. |
| 2273 | return ConstantInt::getTrue(CFP->getContext()); |
| 2274 | } |
Dan Gohman | 6b617a7 | 2010-02-22 04:06:03 +0000 | [diff] [blame] | 2275 | // Check whether the constant is an infinity. |
| 2276 | if (CFP->getValueAPF().isInfinity()) { |
| 2277 | if (CFP->getValueAPF().isNegative()) { |
| 2278 | switch (Pred) { |
| 2279 | case FCmpInst::FCMP_OLT: |
| 2280 | // No value is ordered and less than negative infinity. |
| 2281 | return ConstantInt::getFalse(CFP->getContext()); |
| 2282 | case FCmpInst::FCMP_UGE: |
| 2283 | // All values are unordered with or at least negative infinity. |
| 2284 | return ConstantInt::getTrue(CFP->getContext()); |
| 2285 | default: |
| 2286 | break; |
| 2287 | } |
| 2288 | } else { |
| 2289 | switch (Pred) { |
| 2290 | case FCmpInst::FCMP_OGT: |
| 2291 | // No value is ordered and greater than infinity. |
| 2292 | return ConstantInt::getFalse(CFP->getContext()); |
| 2293 | case FCmpInst::FCMP_ULE: |
| 2294 | // All values are unordered with and at most infinity. |
| 2295 | return ConstantInt::getTrue(CFP->getContext()); |
| 2296 | default: |
| 2297 | break; |
| 2298 | } |
| 2299 | } |
| 2300 | } |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2301 | } |
| 2302 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2303 | |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 2304 | // If the comparison is with the result of a select instruction, check whether |
| 2305 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2306 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2307 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2308 | return V; |
| 2309 | |
| 2310 | // If the comparison is with the result of a phi instruction, check whether |
| 2311 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2312 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2313 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 2314 | return V; |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 2315 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2316 | return 0; |
| 2317 | } |
| 2318 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2319 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2320 | const TargetData *TD, |
| 2321 | const TargetLibraryInfo *TLI, |
| 2322 | const DominatorTree *DT) { |
| 2323 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2326 | /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold |
| 2327 | /// the result. If not, this returns null. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2328 | Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal, |
| 2329 | const TargetData *TD, const DominatorTree *) { |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2330 | // select true, X, Y -> X |
| 2331 | // select false, X, Y -> Y |
| 2332 | if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal)) |
| 2333 | return CB->getZExtValue() ? TrueVal : FalseVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2334 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2335 | // select C, X, X -> X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2336 | if (TrueVal == FalseVal) |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2337 | return TrueVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2338 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2339 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 2340 | if (isa<Constant>(TrueVal)) |
| 2341 | return TrueVal; |
| 2342 | return FalseVal; |
| 2343 | } |
Dan Gohman | 68c0dbc | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 2344 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 2345 | return FalseVal; |
| 2346 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 2347 | return TrueVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2348 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2349 | return 0; |
| 2350 | } |
| 2351 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2352 | /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can |
| 2353 | /// fold the result. If not, this returns null. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2354 | Value *llvm::SimplifyGEPInst(ArrayRef<Value *> Ops, const TargetData *TD, |
| 2355 | const DominatorTree *) { |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2356 | // The type of the GEP pointer operand. |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2357 | PointerType *PtrTy = dyn_cast<PointerType>(Ops[0]->getType()); |
| 2358 | // The GEP pointer operand is not a pointer, it's a vector of pointers. |
| 2359 | if (!PtrTy) |
| 2360 | return 0; |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2361 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2362 | // getelementptr P -> P. |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2363 | if (Ops.size() == 1) |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2364 | return Ops[0]; |
| 2365 | |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2366 | if (isa<UndefValue>(Ops[0])) { |
| 2367 | // Compute the (pointer) type returned by the GEP instruction. |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2368 | Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1)); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2369 | Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace()); |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2370 | return UndefValue::get(GEPTy); |
| 2371 | } |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2372 | |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2373 | if (Ops.size() == 2) { |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2374 | // getelementptr P, 0 -> P. |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2375 | if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1])) |
| 2376 | if (C->isZero()) |
| 2377 | return Ops[0]; |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2378 | // getelementptr P, N -> P if P points to a type of zero size. |
| 2379 | if (TD) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2380 | Type *Ty = PtrTy->getElementType(); |
Duncan Sands | a63395a | 2010-11-22 16:32:50 +0000 | [diff] [blame] | 2381 | if (Ty->isSized() && TD->getTypeAllocSize(Ty) == 0) |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2382 | return Ops[0]; |
| 2383 | } |
| 2384 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2385 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2386 | // Check to see if this is constant foldable. |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2387 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2388 | if (!isa<Constant>(Ops[i])) |
| 2389 | return 0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2390 | |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2391 | return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), Ops.slice(1)); |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2394 | /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we |
| 2395 | /// can fold the result. If not, this returns null. |
| 2396 | Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 2397 | ArrayRef<unsigned> Idxs, |
| 2398 | const TargetData *, |
| 2399 | const DominatorTree *) { |
| 2400 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 2401 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 2402 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 2403 | |
| 2404 | // insertvalue x, undef, n -> x |
| 2405 | if (match(Val, m_Undef())) |
| 2406 | return Agg; |
| 2407 | |
| 2408 | // insertvalue x, (extractvalue y, n), n |
| 2409 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | ae707bd | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 2410 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 2411 | EV->getIndices() == Idxs) { |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2412 | // insertvalue undef, (extractvalue y, n), n -> y |
| 2413 | if (match(Agg, m_Undef())) |
| 2414 | return EV->getAggregateOperand(); |
| 2415 | |
| 2416 | // insertvalue y, (extractvalue y, n), n -> y |
| 2417 | if (Agg == EV->getAggregateOperand()) |
| 2418 | return Agg; |
| 2419 | } |
| 2420 | |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2424 | /// SimplifyPHINode - See if we can fold the given phi. If not, returns null. |
| 2425 | static Value *SimplifyPHINode(PHINode *PN, const DominatorTree *DT) { |
| 2426 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 2427 | // with the common value. |
| 2428 | Value *CommonValue = 0; |
| 2429 | bool HasUndefInput = false; |
| 2430 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 2431 | Value *Incoming = PN->getIncomingValue(i); |
| 2432 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 2433 | if (Incoming == PN) continue; |
| 2434 | if (isa<UndefValue>(Incoming)) { |
| 2435 | // Remember that we saw an undef value, but otherwise ignore them. |
| 2436 | HasUndefInput = true; |
| 2437 | continue; |
| 2438 | } |
| 2439 | if (CommonValue && Incoming != CommonValue) |
| 2440 | return 0; // Not the same, bail out. |
| 2441 | CommonValue = Incoming; |
| 2442 | } |
| 2443 | |
| 2444 | // If CommonValue is null then all of the incoming values were either undef or |
| 2445 | // equal to the phi node itself. |
| 2446 | if (!CommonValue) |
| 2447 | return UndefValue::get(PN->getType()); |
| 2448 | |
| 2449 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 2450 | // instruction, we cannot return X as the result of the PHI node unless it |
| 2451 | // dominates the PHI block. |
| 2452 | if (HasUndefInput) |
| 2453 | return ValueDominatesPHI(CommonValue, PN, DT) ? CommonValue : 0; |
| 2454 | |
| 2455 | return CommonValue; |
| 2456 | } |
| 2457 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2458 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2459 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2460 | /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can |
| 2461 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2462 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2463 | const TargetData *TD, |
| 2464 | const TargetLibraryInfo *TLI, |
| 2465 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 2466 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2467 | switch (Opcode) { |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2468 | case Instruction::Add: |
Duncan Sands | ffeb98a | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2469 | return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2470 | TD, TLI, DT, MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2471 | case Instruction::Sub: |
Duncan Sands | ffeb98a | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2472 | return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2473 | TD, TLI, DT, MaxRecurse); |
| 2474 | case Instruction::Mul: return SimplifyMulInst (LHS, RHS, TD, TLI, DT, |
| 2475 | MaxRecurse); |
| 2476 | case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, TD, TLI, DT, |
| 2477 | MaxRecurse); |
| 2478 | case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, TD, TLI, DT, |
| 2479 | MaxRecurse); |
| 2480 | case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, TD, TLI, DT, |
| 2481 | MaxRecurse); |
| 2482 | case Instruction::SRem: return SimplifySRemInst(LHS, RHS, TD, TLI, DT, |
| 2483 | MaxRecurse); |
| 2484 | case Instruction::URem: return SimplifyURemInst(LHS, RHS, TD, TLI, DT, |
| 2485 | MaxRecurse); |
| 2486 | case Instruction::FRem: return SimplifyFRemInst(LHS, RHS, TD, TLI, DT, |
| 2487 | MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2488 | case Instruction::Shl: |
Duncan Sands | ffeb98a | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2489 | return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2490 | TD, TLI, DT, MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2491 | case Instruction::LShr: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2492 | return SimplifyLShrInst(LHS, RHS, /*isExact*/false, TD, TLI, DT, |
| 2493 | MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2494 | case Instruction::AShr: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2495 | return SimplifyAShrInst(LHS, RHS, /*isExact*/false, TD, TLI, DT, |
| 2496 | MaxRecurse); |
| 2497 | case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, TLI, DT, |
| 2498 | MaxRecurse); |
| 2499 | case Instruction::Or: return SimplifyOrInst (LHS, RHS, TD, TLI, DT, |
| 2500 | MaxRecurse); |
| 2501 | case Instruction::Xor: return SimplifyXorInst(LHS, RHS, TD, TLI, DT, |
| 2502 | MaxRecurse); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2503 | default: |
| 2504 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 2505 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 2506 | Constant *COps[] = {CLHS, CRHS}; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2507 | return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, TD, TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2508 | } |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2509 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2510 | // If the operation is associative, try some generic simplifications. |
| 2511 | if (Instruction::isAssociative(Opcode)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2512 | if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, TD, TLI, DT, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2513 | MaxRecurse)) |
| 2514 | return V; |
| 2515 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2516 | // If the operation is with the result of a select instruction, check whether |
| 2517 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2518 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2519 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, TLI, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2520 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2521 | return V; |
| 2522 | |
| 2523 | // If the operation is with the result of a phi instruction, check whether |
| 2524 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2525 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2526 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, TLI, DT, |
| 2527 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2528 | return V; |
| 2529 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2530 | return 0; |
| 2531 | } |
| 2532 | } |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2533 | |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2534 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2535 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 2536 | const DominatorTree *DT) { |
| 2537 | return ::SimplifyBinOp(Opcode, LHS, RHS, TD, TLI, DT, RecursionLimit); |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2540 | /// SimplifyCmpInst - Given operands for a CmpInst, see if we can |
| 2541 | /// fold the result. |
| 2542 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2543 | const TargetData *TD, |
| 2544 | const TargetLibraryInfo *TLI, |
| 2545 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 2546 | unsigned MaxRecurse) { |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2547 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2548 | return SimplifyICmpInst(Predicate, LHS, RHS, TD, TLI, DT, MaxRecurse); |
| 2549 | return SimplifyFCmpInst(Predicate, LHS, RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2553 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 2554 | const DominatorTree *DT) { |
| 2555 | return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2556 | } |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2557 | |
Dan Gohman | 71d0503 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 2558 | static Value *SimplifyCallInst(CallInst *CI) { |
| 2559 | // call undef -> undef |
| 2560 | if (isa<UndefValue>(CI->getCalledValue())) |
| 2561 | return UndefValue::get(CI->getType()); |
| 2562 | |
| 2563 | return 0; |
| 2564 | } |
| 2565 | |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2566 | /// SimplifyInstruction - See if we can compute a simplified version of this |
| 2567 | /// instruction. If not, this returns null. |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 2568 | Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2569 | const TargetLibraryInfo *TLI, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 2570 | const DominatorTree *DT) { |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2571 | Value *Result; |
| 2572 | |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2573 | switch (I->getOpcode()) { |
| 2574 | default: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2575 | Result = ConstantFoldInstruction(I, TD, TLI); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2576 | break; |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 2577 | case Instruction::Add: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2578 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 2579 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 2580 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2581 | TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2582 | break; |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 2583 | case Instruction::Sub: |
| 2584 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 2585 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 2586 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2587 | TD, TLI, DT); |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 2588 | break; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 2589 | case Instruction::Mul: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2590 | Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 2591 | break; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 2592 | case Instruction::SDiv: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2593 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 2594 | break; |
| 2595 | case Instruction::UDiv: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2596 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 2597 | break; |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 2598 | case Instruction::FDiv: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2599 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 2600 | break; |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 2601 | case Instruction::SRem: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2602 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 2603 | break; |
| 2604 | case Instruction::URem: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2605 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 2606 | break; |
| 2607 | case Instruction::FRem: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2608 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 2609 | break; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 2610 | case Instruction::Shl: |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2611 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 2612 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 2613 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2614 | TD, TLI, DT); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 2615 | break; |
| 2616 | case Instruction::LShr: |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2617 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
| 2618 | cast<BinaryOperator>(I)->isExact(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2619 | TD, TLI, DT); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 2620 | break; |
| 2621 | case Instruction::AShr: |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2622 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
| 2623 | cast<BinaryOperator>(I)->isExact(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2624 | TD, TLI, DT); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 2625 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2626 | case Instruction::And: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2627 | Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2628 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2629 | case Instruction::Or: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2630 | Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2631 | break; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2632 | case Instruction::Xor: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2633 | Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2634 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2635 | case Instruction::ICmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2636 | Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2637 | I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2638 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2639 | case Instruction::FCmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2640 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2641 | I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2642 | break; |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2643 | case Instruction::Select: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2644 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
| 2645 | I->getOperand(2), TD, DT); |
| 2646 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2647 | case Instruction::GetElementPtr: { |
| 2648 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2649 | Result = SimplifyGEPInst(Ops, TD, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2650 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2651 | } |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2652 | case Instruction::InsertValue: { |
| 2653 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 2654 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 2655 | IV->getInsertedValueOperand(), |
| 2656 | IV->getIndices(), TD, DT); |
| 2657 | break; |
| 2658 | } |
Duncan Sands | cd6636c | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 2659 | case Instruction::PHI: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2660 | Result = SimplifyPHINode(cast<PHINode>(I), DT); |
| 2661 | break; |
Dan Gohman | 71d0503 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 2662 | case Instruction::Call: |
| 2663 | Result = SimplifyCallInst(cast<CallInst>(I)); |
| 2664 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2665 | } |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2666 | |
| 2667 | /// If called on unreachable code, the above logic may report that the |
| 2668 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 2669 | /// detecting that case here, returning a safe value instead. |
| 2670 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2673 | /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then |
| 2674 | /// delete the From instruction. In addition to a basic RAUW, this does a |
| 2675 | /// recursive simplification of the newly formed instructions. This catches |
| 2676 | /// things where one simplification exposes other opportunities. This only |
| 2677 | /// simplifies and deletes scalar operations, it does not change the CFG. |
| 2678 | /// |
| 2679 | void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 2680 | const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2681 | const TargetLibraryInfo *TLI, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 2682 | const DominatorTree *DT) { |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2683 | assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2684 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2685 | // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that |
| 2686 | // we can know if it gets deleted out from under us or replaced in a |
| 2687 | // recursive simplification. |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2688 | WeakVH FromHandle(From); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2689 | WeakVH ToHandle(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2690 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2691 | while (!From->use_empty()) { |
| 2692 | // Update the instruction to use the new value. |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2693 | Use &TheUse = From->use_begin().getUse(); |
| 2694 | Instruction *User = cast<Instruction>(TheUse.getUser()); |
| 2695 | TheUse = To; |
| 2696 | |
| 2697 | // Check to see if the instruction can be folded due to the operand |
| 2698 | // replacement. For example changing (or X, Y) into (or X, -1) can replace |
| 2699 | // the 'or' with -1. |
| 2700 | Value *SimplifiedVal; |
| 2701 | { |
| 2702 | // Sanity check to make sure 'User' doesn't dangle across |
| 2703 | // SimplifyInstruction. |
| 2704 | AssertingVH<> UserHandle(User); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2705 | |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2706 | SimplifiedVal = SimplifyInstruction(User, TD, TLI, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2707 | if (SimplifiedVal == 0) continue; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2708 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2709 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2710 | // Recursively simplify this user to the new value. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2711 | ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, TLI, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2712 | From = dyn_cast_or_null<Instruction>((Value*)FromHandle); |
| 2713 | To = ToHandle; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2714 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2715 | assert(ToHandle && "To value deleted by recursive simplification?"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2716 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2717 | // If the recursive simplification ended up revisiting and deleting |
| 2718 | // 'From' then we're done. |
| 2719 | if (From == 0) |
| 2720 | return; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2721 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2722 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2723 | // If 'From' has value handles referring to it, do a real RAUW to update them. |
| 2724 | From->replaceAllUsesWith(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2725 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2726 | From->eraseFromParent(); |
| 2727 | } |