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