Chris Lattner | 084a1b5 | 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 | a021988 | 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 | ed6d6c3 | 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 | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SetVector.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ConstantFolding.h" |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 26 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 29 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/GlobalAlias.h" |
| 31 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 32 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 33 | #include "llvm/IR/ValueHandle.h" |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 35 | using namespace llvm::PatternMatch; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 36 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "instsimplify" |
| 38 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 39 | enum { RecursionLimit = 3 }; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 40 | |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 41 | STATISTIC(NumExpand, "Number of expansions"); |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 42 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 43 | |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 44 | namespace { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 45 | struct Query { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 46 | const DataLayout *DL; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 47 | const TargetLibraryInfo *TLI; |
| 48 | const DominatorTree *DT; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 49 | AssumptionTracker *AT; |
| 50 | const Instruction *CxtI; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 51 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 52 | Query(const DataLayout *DL, const TargetLibraryInfo *tli, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 53 | const DominatorTree *dt, AssumptionTracker *at = nullptr, |
| 54 | const Instruction *cxti = nullptr) |
| 55 | : DL(DL), TLI(tli), DT(dt), AT(at), CxtI(cxti) {} |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 56 | }; |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 57 | } // end anonymous namespace |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 58 | |
| 59 | static Value *SimplifyAndInst(Value *, Value *, const Query &, unsigned); |
| 60 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 61 | unsigned); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 62 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 63 | unsigned); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 64 | static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned); |
| 65 | static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 66 | static Value *SimplifyTruncInst(Value *, Type *, const Query &, unsigned); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 67 | |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 68 | /// getFalse - For a boolean type, or a vector of boolean type, return false, or |
| 69 | /// a vector with every element false, as appropriate for the type. |
| 70 | static Constant *getFalse(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 71 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 72 | "Expected i1 type or a vector of i1!"); |
| 73 | return Constant::getNullValue(Ty); |
| 74 | } |
| 75 | |
| 76 | /// getTrue - For a boolean type, or a vector of boolean type, return true, or |
| 77 | /// a vector with every element true, as appropriate for the type. |
| 78 | static Constant *getTrue(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 79 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 80 | "Expected i1 type or a vector of i1!"); |
| 81 | return Constant::getAllOnesValue(Ty); |
| 82 | } |
| 83 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 84 | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
| 85 | static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, |
| 86 | Value *RHS) { |
| 87 | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
| 88 | if (!Cmp) |
| 89 | return false; |
| 90 | CmpInst::Predicate CPred = Cmp->getPredicate(); |
| 91 | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
| 92 | if (CPred == Pred && CLHS == LHS && CRHS == RHS) |
| 93 | return true; |
| 94 | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && |
| 95 | CRHS == LHS; |
| 96 | } |
| 97 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 98 | /// ValueDominatesPHI - Does the given value dominate the specified phi node? |
| 99 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 100 | Instruction *I = dyn_cast<Instruction>(V); |
| 101 | if (!I) |
| 102 | // Arguments and constants dominate all instructions. |
| 103 | return true; |
| 104 | |
Chandler Carruth | 3ffccb3 | 2012-03-21 10:58:47 +0000 | [diff] [blame] | 105 | // If we are processing instructions (and/or basic blocks) that have not been |
| 106 | // fully added to a function, the parent nodes may still be null. Simply |
| 107 | // return the conservative answer in these cases. |
| 108 | if (!I->getParent() || !P->getParent() || !I->getParent()->getParent()) |
| 109 | return false; |
| 110 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 111 | // If we have a DominatorTree then do a precise test. |
Eli Friedman | c8cbd06 | 2012-03-13 01:06:07 +0000 | [diff] [blame] | 112 | if (DT) { |
| 113 | if (!DT->isReachableFromEntry(P->getParent())) |
| 114 | return true; |
| 115 | if (!DT->isReachableFromEntry(I->getParent())) |
| 116 | return false; |
| 117 | return DT->dominates(I, P); |
| 118 | } |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 119 | |
| 120 | // Otherwise, if the instruction is in the entry block, and is not an invoke, |
| 121 | // then it obviously dominates all phi nodes. |
| 122 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
| 123 | !isa<InvokeInst>(I)) |
| 124 | return true; |
| 125 | |
| 126 | return false; |
| 127 | } |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 128 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 129 | /// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning |
| 130 | /// it into "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is |
| 131 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 132 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 133 | /// Returns the simplified value, or null if no simplification was performed. |
| 134 | static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 135 | unsigned OpcToExpand, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 136 | unsigned MaxRecurse) { |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 137 | Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 138 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 139 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 140 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 141 | |
| 142 | // Check whether the expression has the form "(A op' B) op C". |
| 143 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 144 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 145 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 146 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 147 | // Do "A op C" and "B op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 148 | if (Value *L = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) |
| 149 | if (Value *R = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 150 | // They do! Return "L op' R" if it simplifies or is already available. |
| 151 | // If "L op' R" equals "A op' B" then "L op' R" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 152 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 153 | && L == B && R == A)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 154 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 155 | return LHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 156 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 157 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 158 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 159 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 160 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 161 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | // Check whether the expression has the form "A op (B op' C)". |
| 166 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 167 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 168 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 169 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 170 | // Do "A op B" and "A op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 171 | if (Value *L = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) |
| 172 | if (Value *R = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 173 | // They do! Return "L op' R" if it simplifies or is already available. |
| 174 | // If "L op' R" equals "B op' C" then "L op' R" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 175 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 176 | && L == C && R == B)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 177 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 178 | return RHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 179 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 180 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 181 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 182 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 183 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 184 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 188 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 191 | /// SimplifyAssociativeBinOp - Generic simplifications for associative binary |
| 192 | /// operations. Returns the simpler value, or null if none was found. |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 193 | static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 194 | const Query &Q, unsigned MaxRecurse) { |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 195 | Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 196 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 197 | |
| 198 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 199 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 200 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 201 | |
| 202 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 203 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 204 | |
| 205 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 206 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 207 | Value *A = Op0->getOperand(0); |
| 208 | Value *B = Op0->getOperand(1); |
| 209 | Value *C = RHS; |
| 210 | |
| 211 | // Does "B op C" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 212 | if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 213 | // It does! Return "A op V" if it simplifies or is already available. |
| 214 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 215 | if (V == B) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 216 | // Otherwise return "A op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 217 | if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 218 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 219 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 220 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 225 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 226 | Value *A = LHS; |
| 227 | Value *B = Op1->getOperand(0); |
| 228 | Value *C = Op1->getOperand(1); |
| 229 | |
| 230 | // Does "A op B" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 231 | if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 232 | // It does! Return "V op C" if it simplifies or is already available. |
| 233 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 234 | if (V == B) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 235 | // Otherwise return "V op C" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 236 | if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 237 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 238 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 239 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | // The remaining transforms require commutativity as well as associativity. |
| 244 | if (!Instruction::isCommutative(Opcode)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 245 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 246 | |
| 247 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 248 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 249 | Value *A = Op0->getOperand(0); |
| 250 | Value *B = Op0->getOperand(1); |
| 251 | Value *C = RHS; |
| 252 | |
| 253 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 254 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 255 | // It does! Return "V op B" if it simplifies or is already available. |
| 256 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 257 | if (V == A) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 258 | // Otherwise return "V op B" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 259 | if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 260 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 261 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 262 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 267 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 268 | Value *A = LHS; |
| 269 | Value *B = Op1->getOperand(0); |
| 270 | Value *C = Op1->getOperand(1); |
| 271 | |
| 272 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 273 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 274 | // It does! Return "B op V" if it simplifies or is already available. |
| 275 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 276 | if (V == C) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 277 | // Otherwise return "B op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 278 | if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 279 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 280 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 281 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 285 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 288 | /// ThreadBinOpOverSelect - In the case of a binary operation with a select |
| 289 | /// instruction as an operand, try to simplify the binop by seeing whether |
| 290 | /// evaluating it on both branches of the select results in the same value. |
| 291 | /// Returns the common value if so, otherwise returns null. |
| 292 | static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 293 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 294 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 295 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 296 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 297 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 298 | SelectInst *SI; |
| 299 | if (isa<SelectInst>(LHS)) { |
| 300 | SI = cast<SelectInst>(LHS); |
| 301 | } else { |
| 302 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 303 | SI = cast<SelectInst>(RHS); |
| 304 | } |
| 305 | |
| 306 | // Evaluate the BinOp on the true and false branches of the select. |
| 307 | Value *TV; |
| 308 | Value *FV; |
| 309 | if (SI == LHS) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 310 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse); |
| 311 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 312 | } else { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 313 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse); |
| 314 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Duncan Sands | e3c5395 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 317 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 318 | // If they both failed to simplify then return null. |
| 319 | if (TV == FV) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 320 | return TV; |
| 321 | |
| 322 | // If one branch simplified to undef, return the other one. |
| 323 | if (TV && isa<UndefValue>(TV)) |
| 324 | return FV; |
| 325 | if (FV && isa<UndefValue>(FV)) |
| 326 | return TV; |
| 327 | |
| 328 | // If applying the operation did not change the true and false select values, |
| 329 | // then the result of the binop is the select itself. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 330 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 331 | return SI; |
| 332 | |
| 333 | // If one branch simplified and the other did not, and the simplified |
| 334 | // value is equal to the unsimplified one, return the simplified value. |
| 335 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 336 | if ((FV && !TV) || (TV && !FV)) { |
| 337 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 338 | // same as the original operation. |
| 339 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 340 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 341 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 342 | // We already know that "op" is the same as for the simplified value. See |
| 343 | // if the operands match too. If so, return the simplified value. |
| 344 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 345 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 346 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 347 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 348 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 349 | return Simplified; |
| 350 | if (Simplified->isCommutative() && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 351 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 352 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 353 | return Simplified; |
| 354 | } |
| 355 | } |
| 356 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 357 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /// ThreadCmpOverSelect - In the case of a comparison with a select instruction, |
| 361 | /// try to simplify the comparison by seeing whether both branches of the select |
| 362 | /// result in the same value. Returns the common value if so, otherwise returns |
| 363 | /// null. |
| 364 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 365 | Value *RHS, const Query &Q, |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 366 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 367 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 368 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 369 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 370 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 371 | // Make sure the select is on the LHS. |
| 372 | if (!isa<SelectInst>(LHS)) { |
| 373 | std::swap(LHS, RHS); |
| 374 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 375 | } |
| 376 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 377 | SelectInst *SI = cast<SelectInst>(LHS); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 378 | Value *Cond = SI->getCondition(); |
| 379 | Value *TV = SI->getTrueValue(); |
| 380 | Value *FV = SI->getFalseValue(); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 381 | |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 382 | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 383 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 384 | Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 385 | if (TCmp == Cond) { |
| 386 | // It not only simplified, it simplified to the select condition. Replace |
| 387 | // it with 'true'. |
| 388 | TCmp = getTrue(Cond->getType()); |
| 389 | } else if (!TCmp) { |
| 390 | // It didn't simplify. However if "cmp TV, RHS" is equal to the select |
| 391 | // condition then we can replace it with 'true'. Otherwise give up. |
| 392 | if (!isSameCompare(Cond, Pred, TV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 393 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 394 | TCmp = getTrue(Cond->getType()); |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 397 | // Does "cmp FV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 398 | Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 399 | if (FCmp == Cond) { |
| 400 | // It not only simplified, it simplified to the select condition. Replace |
| 401 | // it with 'false'. |
| 402 | FCmp = getFalse(Cond->getType()); |
| 403 | } else if (!FCmp) { |
| 404 | // It didn't simplify. However if "cmp FV, RHS" is equal to the select |
| 405 | // condition then we can replace it with 'false'. Otherwise give up. |
| 406 | if (!isSameCompare(Cond, Pred, FV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 407 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 408 | FCmp = getFalse(Cond->getType()); |
| 409 | } |
| 410 | |
| 411 | // If both sides simplified to the same value, then use it as the result of |
| 412 | // the original comparison. |
| 413 | if (TCmp == FCmp) |
| 414 | return TCmp; |
Duncan Sands | 26641d7 | 2012-02-10 14:31:24 +0000 | [diff] [blame] | 415 | |
| 416 | // The remaining cases only make sense if the select condition has the same |
| 417 | // type as the result of the comparison, so bail out if this is not so. |
| 418 | if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy()) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 419 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 420 | // If the false value simplified to false, then the result of the compare |
| 421 | // is equal to "Cond && TCmp". This also catches the case when the false |
| 422 | // value simplified to false and the true value to true, returning "Cond". |
| 423 | if (match(FCmp, m_Zero())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 424 | if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 425 | return V; |
| 426 | // If the true value simplified to true, then the result of the compare |
| 427 | // is equal to "Cond || FCmp". |
| 428 | if (match(TCmp, m_One())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 429 | if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 430 | return V; |
| 431 | // Finally, if the false value simplified to true and the true value to |
| 432 | // false, then the result of the compare is equal to "!Cond". |
| 433 | if (match(FCmp, m_One()) && match(TCmp, m_Zero())) |
| 434 | if (Value *V = |
| 435 | SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 436 | Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 437 | return V; |
| 438 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 439 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 442 | /// ThreadBinOpOverPHI - In the case of a binary operation with an operand that |
| 443 | /// is a PHI instruction, try to simplify the binop by seeing whether evaluating |
| 444 | /// it on the incoming phi values yields the same result for every value. If so |
| 445 | /// returns the common value, otherwise returns null. |
| 446 | static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 447 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 448 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 449 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 450 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 451 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 452 | PHINode *PI; |
| 453 | if (isa<PHINode>(LHS)) { |
| 454 | PI = cast<PHINode>(LHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 455 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 456 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 457 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 458 | } else { |
| 459 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 460 | PI = cast<PHINode>(RHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 461 | // Bail out if LHS and the phi may be mutually interdependent due to a loop. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 462 | if (!ValueDominatesPHI(LHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 463 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 467 | Value *CommonValue = nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 468 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | f12ba1d | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 469 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 470 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | f12ba1d | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 471 | if (Incoming == PI) continue; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 472 | Value *V = PI == LHS ? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 473 | SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) : |
| 474 | SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 475 | // If the operation failed to simplify, or simplified to a different value |
| 476 | // to previously, then give up. |
| 477 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 478 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 479 | CommonValue = V; |
| 480 | } |
| 481 | |
| 482 | return CommonValue; |
| 483 | } |
| 484 | |
| 485 | /// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try |
| 486 | /// try to simplify the comparison by seeing whether comparing with all of the |
| 487 | /// incoming phi values yields the same result every time. If so returns the |
| 488 | /// common result, otherwise returns null. |
| 489 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 490 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 491 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 492 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 493 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 494 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 495 | // Make sure the phi is on the LHS. |
| 496 | if (!isa<PHINode>(LHS)) { |
| 497 | std::swap(LHS, RHS); |
| 498 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 499 | } |
| 500 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 501 | PHINode *PI = cast<PHINode>(LHS); |
| 502 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 503 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 504 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 505 | return nullptr; |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 506 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 507 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 508 | Value *CommonValue = nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 509 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | f12ba1d | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 510 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 511 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | f12ba1d | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 512 | if (Incoming == PI) continue; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 513 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 514 | // If the operation failed to simplify, or simplified to a different value |
| 515 | // to previously, then give up. |
| 516 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 517 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 518 | CommonValue = V; |
| 519 | } |
| 520 | |
| 521 | return CommonValue; |
| 522 | } |
| 523 | |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 524 | /// SimplifyAddInst - Given operands for an Add, see if we can |
| 525 | /// fold the result. If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 526 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 527 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 528 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 529 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 530 | Constant *Ops[] = { CLHS, CRHS }; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 531 | return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(), Ops, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 532 | Q.DL, Q.TLI); |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 533 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 534 | |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 535 | // Canonicalize the constant to the RHS. |
| 536 | std::swap(Op0, Op1); |
| 537 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 538 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 539 | // X + undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 540 | if (match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 541 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 542 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 543 | // X + 0 -> X |
| 544 | if (match(Op1, m_Zero())) |
| 545 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 546 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 547 | // X + (Y - X) -> Y |
| 548 | // (Y - X) + X -> Y |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 549 | // Eg: X + -X -> 0 |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 550 | Value *Y = nullptr; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 551 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 552 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 553 | return Y; |
| 554 | |
| 555 | // X + ~X -> -1 since ~X = -X-1 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 556 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 557 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 558 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 559 | |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 560 | /// i1 add -> xor. |
Duncan Sands | 5def0d6 | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 561 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 562 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 563 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 564 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 565 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 566 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 567 | MaxRecurse)) |
| 568 | return V; |
| 569 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 570 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 571 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 572 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 573 | // only if B and C are equal. If B and C are equal then (since we assume |
| 574 | // that operands have already been simplified) "select(cond, B, C)" should |
| 575 | // have been simplified to the common value of B and C already. Analysing |
| 576 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 577 | // for threading over phi nodes. |
| 578 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 579 | return nullptr; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 582 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 583 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 584 | const DominatorTree *DT, AssumptionTracker *AT, |
| 585 | const Instruction *CxtI) { |
| 586 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, |
| 587 | Query (DL, TLI, DT, AT, CxtI), RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 590 | /// \brief Compute the base pointer and cumulative constant offsets for V. |
| 591 | /// |
| 592 | /// This strips all constant offsets off of V, leaving it the base pointer, and |
| 593 | /// accumulates the total constant offset applied in the returned constant. It |
| 594 | /// returns 0 if V is not a pointer, and returns the constant '0' if there are |
| 595 | /// no constant offsets applied. |
Dan Gohman | 36fa839 | 2013-01-31 02:45:26 +0000 | [diff] [blame] | 596 | /// |
| 597 | /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't |
| 598 | /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc. |
| 599 | /// folding. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 600 | static Constant *stripAndComputeConstantOffsets(const DataLayout *DL, |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 601 | Value *&V, |
| 602 | bool AllowNonInbounds = false) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 603 | assert(V->getType()->getScalarType()->isPointerTy()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 604 | |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 605 | // Without DataLayout, just be conservative for now. Theoretically, more could |
| 606 | // be done in this case. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 607 | if (!DL) |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 608 | return ConstantInt::get(IntegerType::get(V->getContext(), 64), 0); |
| 609 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 610 | Type *IntPtrTy = DL->getIntPtrType(V->getType())->getScalarType(); |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 611 | APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 612 | |
| 613 | // Even though we don't look through PHI nodes, we could be called on an |
| 614 | // instruction in an unreachable block, which may be on a cycle. |
| 615 | SmallPtrSet<Value *, 4> Visited; |
| 616 | Visited.insert(V); |
| 617 | do { |
| 618 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 619 | if ((!AllowNonInbounds && !GEP->isInBounds()) || |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 620 | !GEP->accumulateConstantOffset(*DL, Offset)) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 621 | break; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 622 | V = GEP->getPointerOperand(); |
| 623 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 624 | V = cast<Operator>(V)->getOperand(0); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 625 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 626 | if (GA->mayBeOverridden()) |
| 627 | break; |
| 628 | V = GA->getAliasee(); |
| 629 | } else { |
| 630 | break; |
| 631 | } |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 632 | assert(V->getType()->getScalarType()->isPointerTy() && |
| 633 | "Unexpected operand type!"); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 634 | } while (Visited.insert(V).second); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 635 | |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 636 | Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset); |
| 637 | if (V->getType()->isVectorTy()) |
| 638 | return ConstantVector::getSplat(V->getType()->getVectorNumElements(), |
| 639 | OffsetIntPtr); |
| 640 | return OffsetIntPtr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /// \brief Compute the constant difference between two pointer values. |
| 644 | /// If the difference is not a constant, returns zero. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 645 | static Constant *computePointerDifference(const DataLayout *DL, |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 646 | Value *LHS, Value *RHS) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 647 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 648 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 649 | |
| 650 | // If LHS and RHS are not related via constant offsets to the same base |
| 651 | // value, there is nothing we can do here. |
| 652 | if (LHS != RHS) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 653 | return nullptr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 654 | |
| 655 | // Otherwise, the difference of LHS - RHS can be computed as: |
| 656 | // LHS - RHS |
| 657 | // = (LHSOffset + Base) - (RHSOffset + Base) |
| 658 | // = LHSOffset - RHSOffset |
| 659 | return ConstantExpr::getSub(LHSOffset, RHSOffset); |
| 660 | } |
| 661 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 662 | /// SimplifySubInst - Given operands for a Sub, see if we can |
| 663 | /// fold the result. If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 664 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 665 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 666 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) |
| 667 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 668 | Constant *Ops[] = { CLHS, CRHS }; |
| 669 | return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 670 | Ops, Q.DL, Q.TLI); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | // X - undef -> undef |
| 674 | // undef - X -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 675 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 676 | return UndefValue::get(Op0->getType()); |
| 677 | |
| 678 | // X - 0 -> X |
| 679 | if (match(Op1, m_Zero())) |
| 680 | return Op0; |
| 681 | |
| 682 | // X - X -> 0 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 683 | if (Op0 == Op1) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 684 | return Constant::getNullValue(Op0->getType()); |
| 685 | |
David Majnemer | 4efa9ff | 2014-11-22 07:15:16 +0000 | [diff] [blame] | 686 | // 0 - X -> 0 if the sub is NUW. |
| 687 | if (isNUW && match(Op0, m_Zero())) |
| 688 | return Op0; |
David Majnemer | cd4fbcd | 2014-07-31 04:49:18 +0000 | [diff] [blame] | 689 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 690 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 691 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
Dinesh Dwivedi | 99281a0 | 2014-06-26 08:57:33 +0000 | [diff] [blame] | 692 | Value *X = nullptr, *Y = nullptr, *Z = Op1; |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 693 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 694 | // See if "V === Y - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 695 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 696 | // It does! Now see if "X + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 697 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 698 | // It does, we successfully reassociated! |
| 699 | ++NumReassoc; |
| 700 | return W; |
| 701 | } |
| 702 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 703 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 704 | // It does! Now see if "Y + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 705 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 706 | // It does, we successfully reassociated! |
| 707 | ++NumReassoc; |
| 708 | return W; |
| 709 | } |
| 710 | } |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 711 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 712 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 713 | // For example, X - (X + 1) -> -1 |
| 714 | X = Op0; |
| 715 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 716 | // See if "V === X - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 717 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 718 | // It does! Now see if "V - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 719 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 720 | // It does, we successfully reassociated! |
| 721 | ++NumReassoc; |
| 722 | return W; |
| 723 | } |
| 724 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 725 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 726 | // It does! Now see if "V - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 727 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 728 | // It does, we successfully reassociated! |
| 729 | ++NumReassoc; |
| 730 | return W; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 735 | // For example, X - (X - Y) -> Y. |
| 736 | Z = Op0; |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 737 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 738 | // See if "V === Z - X" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 739 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 740 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 741 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 742 | // It does, we successfully reassociated! |
| 743 | ++NumReassoc; |
| 744 | return W; |
| 745 | } |
| 746 | |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 747 | // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. |
| 748 | if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) && |
| 749 | match(Op1, m_Trunc(m_Value(Y)))) |
| 750 | if (X->getType() == Y->getType()) |
| 751 | // See if "V === X - Y" simplifies. |
| 752 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
| 753 | // It does! Now see if "trunc V" simplifies. |
| 754 | if (Value *W = SimplifyTruncInst(V, Op0->getType(), Q, MaxRecurse-1)) |
| 755 | // It does, return the simplified "trunc V". |
| 756 | return W; |
| 757 | |
| 758 | // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 759 | if (match(Op0, m_PtrToInt(m_Value(X))) && |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 760 | match(Op1, m_PtrToInt(m_Value(Y)))) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 761 | if (Constant *Result = computePointerDifference(Q.DL, X, Y)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 762 | return ConstantExpr::getIntegerCast(Result, Op0->getType(), true); |
| 763 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 764 | // i1 sub -> xor. |
| 765 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 766 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 767 | return V; |
| 768 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 769 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 770 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 771 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 772 | // only if B and C are equal. If B and C are equal then (since we assume |
| 773 | // that operands have already been simplified) "select(cond, B, C)" should |
| 774 | // have been simplified to the common value of B and C already. Analysing |
| 775 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 776 | // for threading over phi nodes. |
| 777 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 778 | return nullptr; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 781 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 782 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 783 | const DominatorTree *DT, AssumptionTracker *AT, |
| 784 | const Instruction *CxtI) { |
| 785 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, |
| 786 | Query (DL, TLI, DT, AT, CxtI), RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 789 | /// Given operands for an FAdd, see if we can fold the result. If not, this |
| 790 | /// returns null. |
| 791 | static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 792 | const Query &Q, unsigned MaxRecurse) { |
| 793 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 794 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 795 | Constant *Ops[] = { CLHS, CRHS }; |
| 796 | return ConstantFoldInstOperands(Instruction::FAdd, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 797 | Ops, Q.DL, Q.TLI); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | // Canonicalize the constant to the RHS. |
| 801 | std::swap(Op0, Op1); |
| 802 | } |
| 803 | |
| 804 | // fadd X, -0 ==> X |
| 805 | if (match(Op1, m_NegZero())) |
| 806 | return Op0; |
| 807 | |
| 808 | // fadd X, 0 ==> X, when we know X is not -0 |
| 809 | if (match(Op1, m_Zero()) && |
| 810 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0))) |
| 811 | return Op0; |
| 812 | |
| 813 | // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0 |
| 814 | // where nnan and ninf have to occur at least once somewhere in this |
| 815 | // expression |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 816 | Value *SubOp = nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 817 | if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0)))) |
| 818 | SubOp = Op1; |
| 819 | else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1)))) |
| 820 | SubOp = Op0; |
| 821 | if (SubOp) { |
| 822 | Instruction *FSub = cast<Instruction>(SubOp); |
| 823 | if ((FMF.noNaNs() || FSub->hasNoNaNs()) && |
| 824 | (FMF.noInfs() || FSub->hasNoInfs())) |
| 825 | return Constant::getNullValue(Op0->getType()); |
| 826 | } |
| 827 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 828 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /// Given operands for an FSub, see if we can fold the result. If not, this |
| 832 | /// returns null. |
| 833 | static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 834 | const Query &Q, unsigned MaxRecurse) { |
| 835 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 836 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 837 | Constant *Ops[] = { CLHS, CRHS }; |
| 838 | return ConstantFoldInstOperands(Instruction::FSub, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 839 | Ops, Q.DL, Q.TLI); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
| 843 | // fsub X, 0 ==> X |
| 844 | if (match(Op1, m_Zero())) |
| 845 | return Op0; |
| 846 | |
| 847 | // fsub X, -0 ==> X, when we know X is not -0 |
| 848 | if (match(Op1, m_NegZero()) && |
| 849 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0))) |
| 850 | return Op0; |
| 851 | |
| 852 | // fsub 0, (fsub -0.0, X) ==> X |
| 853 | Value *X; |
| 854 | if (match(Op0, m_AnyZero())) { |
| 855 | if (match(Op1, m_FSub(m_NegZero(), m_Value(X)))) |
| 856 | return X; |
| 857 | if (FMF.noSignedZeros() && match(Op1, m_FSub(m_AnyZero(), m_Value(X)))) |
| 858 | return X; |
| 859 | } |
| 860 | |
| 861 | // fsub nnan ninf x, x ==> 0.0 |
| 862 | if (FMF.noNaNs() && FMF.noInfs() && Op0 == Op1) |
| 863 | return Constant::getNullValue(Op0->getType()); |
| 864 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 865 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 868 | /// Given the operands for an FMul, see if we can fold the result |
| 869 | static Value *SimplifyFMulInst(Value *Op0, Value *Op1, |
| 870 | FastMathFlags FMF, |
| 871 | const Query &Q, |
| 872 | unsigned MaxRecurse) { |
| 873 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 874 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 875 | Constant *Ops[] = { CLHS, CRHS }; |
| 876 | return ConstantFoldInstOperands(Instruction::FMul, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 877 | Ops, Q.DL, Q.TLI); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 878 | } |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 879 | |
| 880 | // Canonicalize the constant to the RHS. |
| 881 | std::swap(Op0, Op1); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 884 | // fmul X, 1.0 ==> X |
| 885 | if (match(Op1, m_FPOne())) |
| 886 | return Op0; |
| 887 | |
| 888 | // fmul nnan nsz X, 0 ==> 0 |
| 889 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero())) |
| 890 | return Op1; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 891 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 892 | return nullptr; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 895 | /// SimplifyMulInst - Given operands for a Mul, see if we can |
| 896 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 897 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q, |
| 898 | unsigned MaxRecurse) { |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 899 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 900 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 901 | Constant *Ops[] = { CLHS, CRHS }; |
| 902 | return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 903 | Ops, Q.DL, Q.TLI); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | // Canonicalize the constant to the RHS. |
| 907 | std::swap(Op0, Op1); |
| 908 | } |
| 909 | |
| 910 | // X * undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 911 | if (match(Op1, m_Undef())) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 912 | return Constant::getNullValue(Op0->getType()); |
| 913 | |
| 914 | // X * 0 -> 0 |
| 915 | if (match(Op1, m_Zero())) |
| 916 | return Op1; |
| 917 | |
| 918 | // X * 1 -> X |
| 919 | if (match(Op1, m_One())) |
| 920 | return Op0; |
| 921 | |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 922 | // (X / Y) * Y -> X if the division is exact. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 923 | Value *X = nullptr; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 924 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 925 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 926 | return X; |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 927 | |
Nick Lewycky | b89d9a4 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 928 | // i1 mul -> and. |
Duncan Sands | 5def0d6 | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 929 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 930 | if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 931 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 932 | |
| 933 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 934 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 935 | MaxRecurse)) |
| 936 | return V; |
| 937 | |
| 938 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 939 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 940 | Q, MaxRecurse)) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 941 | return V; |
| 942 | |
| 943 | // If the operation is with the result of a select instruction, check whether |
| 944 | // operating on either branch of the select always yields the same value. |
| 945 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 946 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 947 | MaxRecurse)) |
| 948 | return V; |
| 949 | |
| 950 | // If the operation is with the result of a phi instruction, check whether |
| 951 | // operating on all incoming values of the phi always yields the same value. |
| 952 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 953 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 954 | MaxRecurse)) |
| 955 | return V; |
| 956 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 957 | return nullptr; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 960 | Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 961 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 962 | const DominatorTree *DT, AssumptionTracker *AT, |
| 963 | const Instruction *CxtI) { |
| 964 | return ::SimplifyFAddInst(Op0, Op1, FMF, Query (DL, TLI, DT, AT, CxtI), |
| 965 | RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 969 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 970 | const DominatorTree *DT, AssumptionTracker *AT, |
| 971 | const Instruction *CxtI) { |
| 972 | return ::SimplifyFSubInst(Op0, Op1, FMF, Query (DL, TLI, DT, AT, CxtI), |
| 973 | RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 976 | Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, |
| 977 | FastMathFlags FMF, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 978 | const DataLayout *DL, |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 979 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 980 | const DominatorTree *DT, |
| 981 | AssumptionTracker *AT, |
| 982 | const Instruction *CxtI) { |
| 983 | return ::SimplifyFMulInst(Op0, Op1, FMF, Query (DL, TLI, DT, AT, CxtI), |
| 984 | RecursionLimit); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 987 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 988 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 989 | const DominatorTree *DT, AssumptionTracker *AT, |
| 990 | const Instruction *CxtI) { |
| 991 | return ::SimplifyMulInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 992 | RecursionLimit); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 995 | /// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can |
| 996 | /// fold the result. If not, this returns null. |
Anders Carlsson | 36c6d23 | 2011-02-05 18:33:43 +0000 | [diff] [blame] | 997 | static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 998 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 999 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1000 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1001 | Constant *Ops[] = { C0, C1 }; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1002 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.DL, Q.TLI); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1006 | bool isSigned = Opcode == Instruction::SDiv; |
| 1007 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1008 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1009 | if (match(Op1, m_Undef())) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1010 | return Op1; |
| 1011 | |
| 1012 | // undef / X -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1013 | if (match(Op0, m_Undef())) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1014 | return Constant::getNullValue(Op0->getType()); |
| 1015 | |
| 1016 | // 0 / X -> 0, we don't need to preserve faults! |
| 1017 | if (match(Op0, m_Zero())) |
| 1018 | return Op0; |
| 1019 | |
| 1020 | // X / 1 -> X |
| 1021 | if (match(Op1, m_One())) |
| 1022 | return Op0; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1023 | |
| 1024 | if (Op0->getType()->isIntegerTy(1)) |
| 1025 | // It can't be division by zero, hence it must be division by one. |
| 1026 | return Op0; |
| 1027 | |
| 1028 | // X / X -> 1 |
| 1029 | if (Op0 == Op1) |
| 1030 | return ConstantInt::get(Op0->getType(), 1); |
| 1031 | |
| 1032 | // (X * Y) / Y -> X if the multiplication does not overflow. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1033 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1034 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 1035 | if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1 |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1036 | OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); |
Duncan Sands | 5747aba | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 1037 | // If the Mul knows it does not overflow, then we are good to go. |
| 1038 | if ((isSigned && Mul->hasNoSignedWrap()) || |
| 1039 | (!isSigned && Mul->hasNoUnsignedWrap())) |
| 1040 | return X; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1041 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 1042 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 1043 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 1044 | return X; |
| 1045 | } |
| 1046 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1047 | // (X rem Y) / Y -> 0 |
| 1048 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1049 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 1050 | return Constant::getNullValue(Op0->getType()); |
| 1051 | |
David Majnemer | cb9d596 | 2014-10-11 10:20:01 +0000 | [diff] [blame] | 1052 | // (X /u C1) /u C2 -> 0 if C1 * C2 overflow |
| 1053 | ConstantInt *C1, *C2; |
| 1054 | if (!isSigned && match(Op0, m_UDiv(m_Value(X), m_ConstantInt(C1))) && |
| 1055 | match(Op1, m_ConstantInt(C2))) { |
| 1056 | bool Overflow; |
| 1057 | C1->getValue().umul_ov(C2->getValue(), Overflow); |
| 1058 | if (Overflow) |
| 1059 | return Constant::getNullValue(Op0->getType()); |
| 1060 | } |
| 1061 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1062 | // If the operation is with the result of a select instruction, check whether |
| 1063 | // operating on either branch of the select always yields the same value. |
| 1064 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1065 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1066 | return V; |
| 1067 | |
| 1068 | // If the operation is with the result of a phi instruction, check whether |
| 1069 | // operating on all incoming values of the phi always yields the same value. |
| 1070 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1071 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1072 | return V; |
| 1073 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1074 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /// SimplifySDivInst - Given operands for an SDiv, see if we can |
| 1078 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1079 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1080 | unsigned MaxRecurse) { |
| 1081 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1082 | return V; |
| 1083 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1084 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1087 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1088 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1089 | const DominatorTree *DT, |
| 1090 | AssumptionTracker *AT, |
| 1091 | const Instruction *CxtI) { |
| 1092 | return ::SimplifySDivInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 1093 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /// SimplifyUDivInst - Given operands for a UDiv, see if we can |
| 1097 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1098 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1099 | unsigned MaxRecurse) { |
| 1100 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1101 | return V; |
| 1102 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1103 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1106 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1107 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1108 | const DominatorTree *DT, |
| 1109 | AssumptionTracker *AT, |
| 1110 | const Instruction *CxtI) { |
| 1111 | return ::SimplifyUDivInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 1112 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1115 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1116 | unsigned) { |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1117 | // undef / X -> undef (the undef could be a snan). |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1118 | if (match(Op0, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1119 | return Op0; |
| 1120 | |
| 1121 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1122 | if (match(Op1, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1123 | return Op1; |
| 1124 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1125 | return nullptr; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1128 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1129 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1130 | const DominatorTree *DT, |
| 1131 | AssumptionTracker *AT, |
| 1132 | const Instruction *CxtI) { |
| 1133 | return ::SimplifyFDivInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 1134 | RecursionLimit); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1137 | /// SimplifyRem - Given operands for an SRem or URem, see if we can |
| 1138 | /// fold the result. If not, this returns null. |
| 1139 | static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1140 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1141 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1142 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1143 | Constant *Ops[] = { C0, C1 }; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1144 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.DL, Q.TLI); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1148 | // X % undef -> undef |
| 1149 | if (match(Op1, m_Undef())) |
| 1150 | return Op1; |
| 1151 | |
| 1152 | // undef % X -> 0 |
| 1153 | if (match(Op0, m_Undef())) |
| 1154 | return Constant::getNullValue(Op0->getType()); |
| 1155 | |
| 1156 | // 0 % X -> 0, we don't need to preserve faults! |
| 1157 | if (match(Op0, m_Zero())) |
| 1158 | return Op0; |
| 1159 | |
| 1160 | // X % 0 -> undef, we don't need to preserve faults! |
| 1161 | if (match(Op1, m_Zero())) |
| 1162 | return UndefValue::get(Op0->getType()); |
| 1163 | |
| 1164 | // X % 1 -> 0 |
| 1165 | if (match(Op1, m_One())) |
| 1166 | return Constant::getNullValue(Op0->getType()); |
| 1167 | |
| 1168 | if (Op0->getType()->isIntegerTy(1)) |
| 1169 | // It can't be remainder by zero, hence it must be remainder by one. |
| 1170 | return Constant::getNullValue(Op0->getType()); |
| 1171 | |
| 1172 | // X % X -> 0 |
| 1173 | if (Op0 == Op1) |
| 1174 | return Constant::getNullValue(Op0->getType()); |
| 1175 | |
David Majnemer | b435a42 | 2014-09-17 04:16:35 +0000 | [diff] [blame] | 1176 | // (X % Y) % Y -> X % Y |
| 1177 | if ((Opcode == Instruction::SRem && |
| 1178 | match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1179 | (Opcode == Instruction::URem && |
| 1180 | match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1181 | return Op0; |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1182 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1183 | // If the operation is with the result of a select instruction, check whether |
| 1184 | // operating on either branch of the select always yields the same value. |
| 1185 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1186 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1187 | return V; |
| 1188 | |
| 1189 | // If the operation is with the result of a phi instruction, check whether |
| 1190 | // operating on all incoming values of the phi always yields the same value. |
| 1191 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1192 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1193 | return V; |
| 1194 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1195 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /// SimplifySRemInst - Given operands for an SRem, see if we can |
| 1199 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1200 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q, |
| 1201 | unsigned MaxRecurse) { |
| 1202 | if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1203 | return V; |
| 1204 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1205 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1208 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1209 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1210 | const DominatorTree *DT, |
| 1211 | AssumptionTracker *AT, |
| 1212 | const Instruction *CxtI) { |
| 1213 | return ::SimplifySRemInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 1214 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | /// SimplifyURemInst - Given operands for a URem, see if we can |
| 1218 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1219 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1220 | unsigned MaxRecurse) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1221 | if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1222 | return V; |
| 1223 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1224 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1227 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1228 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1229 | const DominatorTree *DT, |
| 1230 | AssumptionTracker *AT, |
| 1231 | const Instruction *CxtI) { |
| 1232 | return ::SimplifyURemInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 1233 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1236 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1237 | unsigned) { |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1238 | // undef % X -> undef (the undef could be a snan). |
| 1239 | if (match(Op0, m_Undef())) |
| 1240 | return Op0; |
| 1241 | |
| 1242 | // X % undef -> undef |
| 1243 | if (match(Op1, m_Undef())) |
| 1244 | return Op1; |
| 1245 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1246 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1249 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1250 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1251 | const DominatorTree *DT, |
| 1252 | AssumptionTracker *AT, |
| 1253 | const Instruction *CxtI) { |
| 1254 | return ::SimplifyFRemInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 1255 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1258 | /// isUndefShift - Returns true if a shift by \c Amount always yields undef. |
| 1259 | static bool isUndefShift(Value *Amount) { |
| 1260 | Constant *C = dyn_cast<Constant>(Amount); |
| 1261 | if (!C) |
| 1262 | return false; |
| 1263 | |
| 1264 | // X shift by undef -> undef because it may shift by the bitwidth. |
| 1265 | if (isa<UndefValue>(C)) |
| 1266 | return true; |
| 1267 | |
| 1268 | // Shifting by the bitwidth or more is undefined. |
| 1269 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1270 | if (CI->getValue().getLimitedValue() >= |
| 1271 | CI->getType()->getScalarSizeInBits()) |
| 1272 | return true; |
| 1273 | |
| 1274 | // If all lanes of a vector shift are undefined the whole shift is. |
| 1275 | if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) { |
| 1276 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; ++I) |
| 1277 | if (!isUndefShift(C->getAggregateElement(I))) |
| 1278 | return false; |
| 1279 | return true; |
| 1280 | } |
| 1281 | |
| 1282 | return false; |
| 1283 | } |
| 1284 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1285 | /// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1286 | /// fold the result. If not, this returns null. |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1287 | static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1288 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1289 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1290 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1291 | Constant *Ops[] = { C0, C1 }; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1292 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.DL, Q.TLI); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1296 | // 0 shift by X -> 0 |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1297 | if (match(Op0, m_Zero())) |
| 1298 | return Op0; |
| 1299 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1300 | // X shift by 0 -> X |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1301 | if (match(Op1, m_Zero())) |
| 1302 | return Op0; |
| 1303 | |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1304 | // Fold undefined shifts. |
| 1305 | if (isUndefShift(Op1)) |
| 1306 | return UndefValue::get(Op0->getType()); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1307 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1308 | // If the operation is with the result of a select instruction, check whether |
| 1309 | // operating on either branch of the select always yields the same value. |
| 1310 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1311 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1312 | return V; |
| 1313 | |
| 1314 | // If the operation is with the result of a phi instruction, check whether |
| 1315 | // operating on all incoming values of the phi always yields the same value. |
| 1316 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1317 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1318 | return V; |
| 1319 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1320 | return nullptr; |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1323 | /// \brief Given operands for an Shl, LShr or AShr, see if we can |
| 1324 | /// fold the result. If not, this returns null. |
| 1325 | static Value *SimplifyRightShift(unsigned Opcode, Value *Op0, Value *Op1, |
| 1326 | bool isExact, const Query &Q, |
| 1327 | unsigned MaxRecurse) { |
| 1328 | if (Value *V = SimplifyShift(Opcode, Op0, Op1, Q, MaxRecurse)) |
| 1329 | return V; |
| 1330 | |
| 1331 | // X >> X -> 0 |
| 1332 | if (Op0 == Op1) |
| 1333 | return Constant::getNullValue(Op0->getType()); |
| 1334 | |
| 1335 | // The low bit cannot be shifted out of an exact shift if it is set. |
| 1336 | if (isExact) { |
| 1337 | unsigned BitWidth = Op0->getType()->getScalarSizeInBits(); |
| 1338 | APInt Op0KnownZero(BitWidth, 0); |
| 1339 | APInt Op0KnownOne(BitWidth, 0); |
| 1340 | computeKnownBits(Op0, Op0KnownZero, Op0KnownOne, Q.DL, /*Depth=*/0, Q.AT, Q.CxtI, |
| 1341 | Q.DT); |
| 1342 | if (Op0KnownOne[0]) |
| 1343 | return Op0; |
| 1344 | } |
| 1345 | |
| 1346 | return nullptr; |
| 1347 | } |
| 1348 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1349 | /// SimplifyShlInst - Given operands for an Shl, see if we can |
| 1350 | /// fold the result. If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1351 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1352 | const Query &Q, unsigned MaxRecurse) { |
| 1353 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1354 | return V; |
| 1355 | |
| 1356 | // undef << X -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1357 | if (match(Op0, m_Undef())) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1358 | return Constant::getNullValue(Op0->getType()); |
| 1359 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1360 | // (X >> A) << A -> X |
| 1361 | Value *X; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1362 | if (match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1))))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1363 | return X; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1364 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1367 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1368 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1369 | const DominatorTree *DT, AssumptionTracker *AT, |
| 1370 | const Instruction *CxtI) { |
| 1371 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query (DL, TLI, DT, AT, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1372 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | /// SimplifyLShrInst - Given operands for an LShr, see if we can |
| 1376 | /// fold the result. If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1377 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1378 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1379 | if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q, |
| 1380 | MaxRecurse)) |
| 1381 | return V; |
David Majnemer | a80fed7 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1382 | |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1383 | // undef >>l X -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1384 | if (match(Op0, m_Undef())) |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1385 | return Constant::getNullValue(Op0->getType()); |
| 1386 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1387 | // (X << A) >> A -> X |
| 1388 | Value *X; |
David Majnemer | 4f43837 | 2014-11-04 17:38:50 +0000 | [diff] [blame] | 1389 | if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1390 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1391 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1392 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1395 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1396 | const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1397 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1398 | const DominatorTree *DT, |
| 1399 | AssumptionTracker *AT, |
| 1400 | const Instruction *CxtI) { |
| 1401 | return ::SimplifyLShrInst(Op0, Op1, isExact, Query (DL, TLI, DT, AT, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1402 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | /// SimplifyAShrInst - Given operands for an AShr, see if we can |
| 1406 | /// fold the result. If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1407 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1408 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1409 | if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q, |
| 1410 | MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1411 | return V; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1412 | |
| 1413 | // all ones >>a X -> all ones |
| 1414 | if (match(Op0, m_AllOnes())) |
| 1415 | return Op0; |
| 1416 | |
| 1417 | // undef >>a X -> all ones |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1418 | if (match(Op0, m_Undef())) |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1419 | return Constant::getAllOnesValue(Op0->getType()); |
| 1420 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1421 | // (X << A) >> A -> X |
| 1422 | Value *X; |
David Majnemer | 2de97fc | 2014-11-04 17:47:13 +0000 | [diff] [blame] | 1423 | if (match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1424 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1425 | |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1426 | // Arithmetic shifting an all-sign-bit value is a no-op. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1427 | unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AT, Q.CxtI, Q.DT); |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1428 | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
| 1429 | return Op0; |
| 1430 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1431 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1434 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1435 | const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1436 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1437 | const DominatorTree *DT, |
| 1438 | AssumptionTracker *AT, |
| 1439 | const Instruction *CxtI) { |
| 1440 | return ::SimplifyAShrInst(Op0, Op1, isExact, Query (DL, TLI, DT, AT, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1441 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1444 | // Simplify (and (icmp ...) (icmp ...)) to true when we can tell that the range |
| 1445 | // of possible values cannot be satisfied. |
| 1446 | static Value *SimplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
| 1447 | ICmpInst::Predicate Pred0, Pred1; |
| 1448 | ConstantInt *CI1, *CI2; |
| 1449 | Value *V; |
| 1450 | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_ConstantInt(CI1)), |
| 1451 | m_ConstantInt(CI2)))) |
| 1452 | return nullptr; |
| 1453 | |
| 1454 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Specific(CI1)))) |
| 1455 | return nullptr; |
| 1456 | |
| 1457 | Type *ITy = Op0->getType(); |
| 1458 | |
| 1459 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
| 1460 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1461 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1462 | |
| 1463 | const APInt &CI1V = CI1->getValue(); |
| 1464 | const APInt &CI2V = CI2->getValue(); |
| 1465 | const APInt Delta = CI2V - CI1V; |
| 1466 | if (CI1V.isStrictlyPositive()) { |
| 1467 | if (Delta == 2) { |
| 1468 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT) |
| 1469 | return getFalse(ITy); |
| 1470 | if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1471 | return getFalse(ITy); |
| 1472 | } |
| 1473 | if (Delta == 1) { |
| 1474 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT) |
| 1475 | return getFalse(ITy); |
| 1476 | if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1477 | return getFalse(ITy); |
| 1478 | } |
| 1479 | } |
| 1480 | if (CI1V.getBoolValue() && isNUW) { |
| 1481 | if (Delta == 2) |
| 1482 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT) |
| 1483 | return getFalse(ITy); |
| 1484 | if (Delta == 1) |
| 1485 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT) |
| 1486 | return getFalse(ITy); |
| 1487 | } |
| 1488 | |
| 1489 | return nullptr; |
| 1490 | } |
| 1491 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1492 | /// SimplifyAndInst - Given operands for an And, see if we can |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1493 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1494 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1495 | unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1496 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1497 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1498 | Constant *Ops[] = { CLHS, CRHS }; |
| 1499 | return ConstantFoldInstOperands(Instruction::And, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1500 | Ops, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1501 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1502 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1503 | // Canonicalize the constant to the RHS. |
| 1504 | std::swap(Op0, Op1); |
| 1505 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1506 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1507 | // X & undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1508 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1509 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1510 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1511 | // X & X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1512 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1513 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1514 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1515 | // X & 0 = 0 |
| 1516 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1517 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1518 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1519 | // X & -1 = X |
| 1520 | if (match(Op1, m_AllOnes())) |
| 1521 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1522 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1523 | // A & ~A = ~A & A = 0 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1524 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1525 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1526 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1528 | // (A | ?) & A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1529 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1530 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1531 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1532 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1533 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1534 | // A & (A | ?) = A |
| 1535 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1536 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1537 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1538 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1539 | // A & (-A) = A if A is a power of two or zero. |
| 1540 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1541 | match(Op1, m_Neg(m_Specific(Op0)))) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1542 | if (isKnownToBeAPowerOfTwo(Op0, /*OrZero*/true, 0, Q.AT, Q.CxtI, Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1543 | return Op0; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1544 | if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true, 0, Q.AT, Q.CxtI, Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1545 | return Op1; |
| 1546 | } |
| 1547 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1548 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1549 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1550 | if (Value *V = SimplifyAndOfICmps(ICILHS, ICIRHS)) |
| 1551 | return V; |
| 1552 | if (Value *V = SimplifyAndOfICmps(ICIRHS, ICILHS)) |
| 1553 | return V; |
| 1554 | } |
| 1555 | } |
| 1556 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1557 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1558 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, |
| 1559 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1560 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1561 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1562 | // And distributes over Or. Try some generic simplifications based on this. |
| 1563 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1564 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1565 | return V; |
| 1566 | |
| 1567 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1568 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1569 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1570 | return V; |
| 1571 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1572 | // If the operation is with the result of a select instruction, check whether |
| 1573 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1574 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1575 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, |
| 1576 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1577 | return V; |
| 1578 | |
| 1579 | // If the operation is with the result of a phi instruction, check whether |
| 1580 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1581 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1582 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1583 | MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1584 | return V; |
| 1585 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1586 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1589 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1590 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1591 | const DominatorTree *DT, AssumptionTracker *AT, |
| 1592 | const Instruction *CxtI) { |
| 1593 | return ::SimplifyAndInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 1594 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1597 | // Simplify (or (icmp ...) (icmp ...)) to true when we can tell that the union |
| 1598 | // contains all possible values. |
| 1599 | static Value *SimplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
| 1600 | ICmpInst::Predicate Pred0, Pred1; |
| 1601 | ConstantInt *CI1, *CI2; |
| 1602 | Value *V; |
| 1603 | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_ConstantInt(CI1)), |
| 1604 | m_ConstantInt(CI2)))) |
| 1605 | return nullptr; |
| 1606 | |
| 1607 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Specific(CI1)))) |
| 1608 | return nullptr; |
| 1609 | |
| 1610 | Type *ITy = Op0->getType(); |
| 1611 | |
| 1612 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
| 1613 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1614 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1615 | |
| 1616 | const APInt &CI1V = CI1->getValue(); |
| 1617 | const APInt &CI2V = CI2->getValue(); |
| 1618 | const APInt Delta = CI2V - CI1V; |
| 1619 | if (CI1V.isStrictlyPositive()) { |
| 1620 | if (Delta == 2) { |
| 1621 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE) |
| 1622 | return getTrue(ITy); |
| 1623 | if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1624 | return getTrue(ITy); |
| 1625 | } |
| 1626 | if (Delta == 1) { |
| 1627 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE) |
| 1628 | return getTrue(ITy); |
| 1629 | if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1630 | return getTrue(ITy); |
| 1631 | } |
| 1632 | } |
| 1633 | if (CI1V.getBoolValue() && isNUW) { |
| 1634 | if (Delta == 2) |
| 1635 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE) |
| 1636 | return getTrue(ITy); |
| 1637 | if (Delta == 1) |
| 1638 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE) |
| 1639 | return getTrue(ITy); |
| 1640 | } |
| 1641 | |
| 1642 | return nullptr; |
| 1643 | } |
| 1644 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1645 | /// SimplifyOrInst - Given operands for an Or, see if we can |
| 1646 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1647 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const Query &Q, |
| 1648 | unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1649 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1650 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1651 | Constant *Ops[] = { CLHS, CRHS }; |
| 1652 | return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1653 | Ops, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1654 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1655 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1656 | // Canonicalize the constant to the RHS. |
| 1657 | std::swap(Op0, Op1); |
| 1658 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1659 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1660 | // X | undef -> -1 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1661 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1662 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1664 | // X | X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1665 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1666 | return Op0; |
| 1667 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1668 | // X | 0 = X |
| 1669 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1670 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1671 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1672 | // X | -1 = -1 |
| 1673 | if (match(Op1, m_AllOnes())) |
| 1674 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1676 | // A | ~A = ~A | A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1677 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1678 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1679 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1680 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1681 | // (A & ?) | A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1682 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1683 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1684 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1685 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1686 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1687 | // A | (A & ?) = A |
| 1688 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1689 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1690 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1691 | |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1692 | // ~(A & ?) | A = -1 |
| 1693 | if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1694 | (A == Op1 || B == Op1)) |
| 1695 | return Constant::getAllOnesValue(Op1->getType()); |
| 1696 | |
| 1697 | // A | ~(A & ?) = -1 |
| 1698 | if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1699 | (A == Op0 || B == Op0)) |
| 1700 | return Constant::getAllOnesValue(Op0->getType()); |
| 1701 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1702 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1703 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1704 | if (Value *V = SimplifyOrOfICmps(ICILHS, ICIRHS)) |
| 1705 | return V; |
| 1706 | if (Value *V = SimplifyOrOfICmps(ICIRHS, ICILHS)) |
| 1707 | return V; |
| 1708 | } |
| 1709 | } |
| 1710 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1711 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1712 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, |
| 1713 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1714 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1715 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1716 | // Or distributes over And. Try some generic simplifications based on this. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1717 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q, |
| 1718 | MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1719 | return V; |
| 1720 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1721 | // If the operation is with the result of a select instruction, check whether |
| 1722 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1723 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1724 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1725 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1726 | return V; |
| 1727 | |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1728 | // (A & C)|(B & D) |
| 1729 | Value *C = nullptr, *D = nullptr; |
| 1730 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 1731 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
| 1732 | ConstantInt *C1 = dyn_cast<ConstantInt>(C); |
| 1733 | ConstantInt *C2 = dyn_cast<ConstantInt>(D); |
| 1734 | if (C1 && C2 && (C1->getValue() == ~C2->getValue())) { |
| 1735 | // (A & C1)|(B & C2) |
| 1736 | // If we have: ((V + N) & C1) | (V & C2) |
| 1737 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 1738 | // replace with V+N. |
| 1739 | Value *V1, *V2; |
| 1740 | if ((C2->getValue() & (C2->getValue() + 1)) == 0 && // C2 == 0+1+ |
| 1741 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1742 | // Add commutes, try both ways. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1743 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue(), Q.DL, |
| 1744 | 0, Q.AT, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1745 | return A; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1746 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue(), Q.DL, |
| 1747 | 0, Q.AT, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1748 | return A; |
| 1749 | } |
| 1750 | // Or commutes, try both ways. |
| 1751 | if ((C1->getValue() & (C1->getValue() + 1)) == 0 && |
| 1752 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1753 | // Add commutes, try both ways. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1754 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue(), Q.DL, |
| 1755 | 0, Q.AT, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1756 | return B; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1757 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue(), Q.DL, |
| 1758 | 0, Q.AT, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1759 | return B; |
| 1760 | } |
| 1761 | } |
| 1762 | } |
| 1763 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1764 | // If the operation is with the result of a phi instruction, check whether |
| 1765 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1766 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1767 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1768 | return V; |
| 1769 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1770 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1773 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1774 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1775 | const DominatorTree *DT, AssumptionTracker *AT, |
| 1776 | const Instruction *CxtI) { |
| 1777 | return ::SimplifyOrInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 1778 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1779 | } |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1780 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1781 | /// SimplifyXorInst - Given operands for a Xor, see if we can |
| 1782 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1783 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const Query &Q, |
| 1784 | unsigned MaxRecurse) { |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1785 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1786 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1787 | Constant *Ops[] = { CLHS, CRHS }; |
| 1788 | return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1789 | Ops, Q.DL, Q.TLI); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | // Canonicalize the constant to the RHS. |
| 1793 | std::swap(Op0, Op1); |
| 1794 | } |
| 1795 | |
| 1796 | // A ^ undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1797 | if (match(Op1, m_Undef())) |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1798 | return Op1; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1799 | |
| 1800 | // A ^ 0 = A |
| 1801 | if (match(Op1, m_Zero())) |
| 1802 | return Op0; |
| 1803 | |
Eli Friedman | ad3cfe7 | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 1804 | // A ^ A = 0 |
| 1805 | if (Op0 == Op1) |
| 1806 | return Constant::getNullValue(Op0->getType()); |
| 1807 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1808 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1809 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1810 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1811 | return Constant::getAllOnesValue(Op0->getType()); |
| 1812 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1813 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1814 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, |
| 1815 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1816 | return V; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1817 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1818 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1819 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1820 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1821 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1822 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1823 | // have been simplified to the common value of B and C already. Analysing |
| 1824 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1825 | // for threading over phi nodes. |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1826 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1827 | return nullptr; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1830 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1831 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1832 | const DominatorTree *DT, AssumptionTracker *AT, |
| 1833 | const Instruction *CxtI) { |
| 1834 | return ::SimplifyXorInst(Op0, Op1, Query (DL, TLI, DT, AT, CxtI), |
| 1835 | RecursionLimit); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1838 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1839 | return CmpInst::makeCmpResultType(Op->getType()); |
| 1840 | } |
| 1841 | |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1842 | /// ExtractEquivalentCondition - Rummage around inside V looking for something |
| 1843 | /// equivalent to the comparison "LHS Pred RHS". Return such a value if found, |
| 1844 | /// otherwise return null. Helper function for analyzing max/min idioms. |
| 1845 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 1846 | Value *LHS, Value *RHS) { |
| 1847 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 1848 | if (!SI) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1849 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1850 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1851 | if (!Cmp) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1852 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1853 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 1854 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 1855 | return Cmp; |
| 1856 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 1857 | LHS == CmpRHS && RHS == CmpLHS) |
| 1858 | return Cmp; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1859 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 1862 | // A significant optimization not implemented here is assuming that alloca |
| 1863 | // addresses are not equal to incoming argument values. They don't *alias*, |
| 1864 | // as we say, but that doesn't mean they aren't equal, so we take a |
| 1865 | // conservative approach. |
| 1866 | // |
| 1867 | // This is inspired in part by C++11 5.10p1: |
| 1868 | // "Two pointers of the same type compare equal if and only if they are both |
| 1869 | // null, both point to the same function, or both represent the same |
| 1870 | // address." |
| 1871 | // |
| 1872 | // This is pretty permissive. |
| 1873 | // |
| 1874 | // It's also partly due to C11 6.5.9p6: |
| 1875 | // "Two pointers compare equal if and only if both are null pointers, both are |
| 1876 | // pointers to the same object (including a pointer to an object and a |
| 1877 | // subobject at its beginning) or function, both are pointers to one past the |
| 1878 | // last element of the same array object, or one is a pointer to one past the |
| 1879 | // end of one array object and the other is a pointer to the start of a |
NAKAMURA Takumi | 065fd35 | 2013-04-08 23:05:21 +0000 | [diff] [blame] | 1880 | // different array object that happens to immediately follow the first array |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 1881 | // object in the address space.) |
| 1882 | // |
| 1883 | // C11's version is more restrictive, however there's no reason why an argument |
| 1884 | // couldn't be a one-past-the-end value for a stack object in the caller and be |
| 1885 | // equal to the beginning of a stack object in the callee. |
| 1886 | // |
| 1887 | // If the C and C++ standards are ever made sufficiently restrictive in this |
| 1888 | // area, it may be possible to update LLVM's semantics accordingly and reinstate |
| 1889 | // this optimization. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1890 | static Constant *computePointerICmp(const DataLayout *DL, |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1891 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1892 | CmpInst::Predicate Pred, |
| 1893 | Value *LHS, Value *RHS) { |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1894 | // First, skip past any trivial no-ops. |
| 1895 | LHS = LHS->stripPointerCasts(); |
| 1896 | RHS = RHS->stripPointerCasts(); |
| 1897 | |
| 1898 | // A non-null pointer is not equal to a null pointer. |
Benjamin Kramer | fd4777c | 2013-09-24 16:37:51 +0000 | [diff] [blame] | 1899 | if (llvm::isKnownNonNull(LHS, TLI) && isa<ConstantPointerNull>(RHS) && |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1900 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE)) |
| 1901 | return ConstantInt::get(GetCompareTy(LHS), |
| 1902 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1903 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1904 | // We can only fold certain predicates on pointer comparisons. |
| 1905 | switch (Pred) { |
| 1906 | default: |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1907 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Equality comaprisons are easy to fold. |
| 1910 | case CmpInst::ICMP_EQ: |
| 1911 | case CmpInst::ICMP_NE: |
| 1912 | break; |
| 1913 | |
| 1914 | // We can only handle unsigned relational comparisons because 'inbounds' on |
| 1915 | // a GEP only protects against unsigned wrapping. |
| 1916 | case CmpInst::ICMP_UGT: |
| 1917 | case CmpInst::ICMP_UGE: |
| 1918 | case CmpInst::ICMP_ULT: |
| 1919 | case CmpInst::ICMP_ULE: |
| 1920 | // However, we have to switch them to their signed variants to handle |
| 1921 | // negative indices from the base pointer. |
| 1922 | Pred = ICmpInst::getSignedPredicate(Pred); |
| 1923 | break; |
| 1924 | } |
| 1925 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1926 | // Strip off any constant offsets so that we can reason about them. |
| 1927 | // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets |
| 1928 | // here and compare base addresses like AliasAnalysis does, however there are |
| 1929 | // numerous hazards. AliasAnalysis and its utilities rely on special rules |
| 1930 | // governing loads and stores which don't apply to icmps. Also, AliasAnalysis |
| 1931 | // doesn't need to guarantee pointer inequality when it says NoAlias. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1932 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 1933 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1934 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1935 | // If LHS and RHS are related via constant offsets to the same base |
| 1936 | // value, we can replace it with an icmp which just compares the offsets. |
| 1937 | if (LHS == RHS) |
| 1938 | return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1939 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1940 | // Various optimizations for (in)equality comparisons. |
| 1941 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) { |
| 1942 | // Different non-empty allocations that exist at the same time have |
| 1943 | // different addresses (if the program can tell). Global variables always |
| 1944 | // exist, so they always exist during the lifetime of each other and all |
| 1945 | // allocas. Two different allocas usually have different addresses... |
| 1946 | // |
| 1947 | // However, if there's an @llvm.stackrestore dynamically in between two |
| 1948 | // allocas, they may have the same address. It's tempting to reduce the |
| 1949 | // scope of the problem by only looking at *static* allocas here. That would |
| 1950 | // cover the majority of allocas while significantly reducing the likelihood |
| 1951 | // of having an @llvm.stackrestore pop up in the middle. However, it's not |
| 1952 | // actually impossible for an @llvm.stackrestore to pop up in the middle of |
| 1953 | // an entry block. Also, if we have a block that's not attached to a |
| 1954 | // function, we can't tell if it's "static" under the current definition. |
| 1955 | // Theoretically, this problem could be fixed by creating a new kind of |
| 1956 | // instruction kind specifically for static allocas. Such a new instruction |
| 1957 | // could be required to be at the top of the entry block, thus preventing it |
| 1958 | // from being subject to a @llvm.stackrestore. Instcombine could even |
| 1959 | // convert regular allocas into these special allocas. It'd be nifty. |
| 1960 | // However, until then, this problem remains open. |
| 1961 | // |
| 1962 | // So, we'll assume that two non-empty allocas have different addresses |
| 1963 | // for now. |
| 1964 | // |
| 1965 | // With all that, if the offsets are within the bounds of their allocations |
| 1966 | // (and not one-past-the-end! so we can't use inbounds!), and their |
| 1967 | // allocations aren't the same, the pointers are not equal. |
| 1968 | // |
| 1969 | // Note that it's not necessary to check for LHS being a global variable |
| 1970 | // address, due to canonicalization and constant folding. |
| 1971 | if (isa<AllocaInst>(LHS) && |
| 1972 | (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1973 | ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset); |
| 1974 | ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1975 | uint64_t LHSSize, RHSSize; |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1976 | if (LHSOffsetCI && RHSOffsetCI && |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1977 | getObjectSize(LHS, LHSSize, DL, TLI) && |
| 1978 | getObjectSize(RHS, RHSSize, DL, TLI)) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1979 | const APInt &LHSOffsetValue = LHSOffsetCI->getValue(); |
| 1980 | const APInt &RHSOffsetValue = RHSOffsetCI->getValue(); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1981 | if (!LHSOffsetValue.isNegative() && |
| 1982 | !RHSOffsetValue.isNegative() && |
| 1983 | LHSOffsetValue.ult(LHSSize) && |
| 1984 | RHSOffsetValue.ult(RHSSize)) { |
| 1985 | return ConstantInt::get(GetCompareTy(LHS), |
| 1986 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | // Repeat the above check but this time without depending on DataLayout |
| 1991 | // or being able to compute a precise size. |
| 1992 | if (!cast<PointerType>(LHS->getType())->isEmptyTy() && |
| 1993 | !cast<PointerType>(RHS->getType())->isEmptyTy() && |
| 1994 | LHSOffset->isNullValue() && |
| 1995 | RHSOffset->isNullValue()) |
| 1996 | return ConstantInt::get(GetCompareTy(LHS), |
| 1997 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1998 | } |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 1999 | |
| 2000 | // Even if an non-inbounds GEP occurs along the path we can still optimize |
| 2001 | // equality comparisons concerning the result. We avoid walking the whole |
| 2002 | // chain again by starting where the last calls to |
| 2003 | // stripAndComputeConstantOffsets left off and accumulate the offsets. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2004 | Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true); |
| 2005 | Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true); |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2006 | if (LHS == RHS) |
| 2007 | return ConstantExpr::getICmp(Pred, |
| 2008 | ConstantExpr::getAdd(LHSOffset, LHSNoBound), |
| 2009 | ConstantExpr::getAdd(RHSOffset, RHSNoBound)); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | // Otherwise, fail. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2013 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2014 | } |
Chris Lattner | 01990f0 | 2012-02-24 19:01:58 +0000 | [diff] [blame] | 2015 | |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2016 | /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can |
| 2017 | /// fold the result. If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2018 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2019 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 2020 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2021 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2022 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2023 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | cdfb80d | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 2024 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2025 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2026 | |
| 2027 | // If we have a constant, make sure it is on the RHS. |
| 2028 | std::swap(LHS, RHS); |
| 2029 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 2030 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2031 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2032 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2033 | Type *OpTy = LHS->getType(); // The operand type. |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2035 | // icmp X, X -> true/false |
Chris Lattner | 3afc072 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 2036 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 2037 | // because X could be 0. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2038 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2039 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2040 | |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2041 | // Special case logic when the operands have i1 type. |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 2042 | if (OpTy->getScalarType()->isIntegerTy(1)) { |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2043 | switch (Pred) { |
| 2044 | default: break; |
| 2045 | case ICmpInst::ICMP_EQ: |
| 2046 | // X == 1 -> X |
| 2047 | if (match(RHS, m_One())) |
| 2048 | return LHS; |
| 2049 | break; |
| 2050 | case ICmpInst::ICMP_NE: |
| 2051 | // X != 0 -> X |
| 2052 | if (match(RHS, m_Zero())) |
| 2053 | return LHS; |
| 2054 | break; |
| 2055 | case ICmpInst::ICMP_UGT: |
| 2056 | // X >u 0 -> X |
| 2057 | if (match(RHS, m_Zero())) |
| 2058 | return LHS; |
| 2059 | break; |
| 2060 | case ICmpInst::ICMP_UGE: |
| 2061 | // X >=u 1 -> X |
| 2062 | if (match(RHS, m_One())) |
| 2063 | return LHS; |
| 2064 | break; |
| 2065 | case ICmpInst::ICMP_SLT: |
| 2066 | // X <s 0 -> X |
| 2067 | if (match(RHS, m_Zero())) |
| 2068 | return LHS; |
| 2069 | break; |
| 2070 | case ICmpInst::ICMP_SLE: |
| 2071 | // X <=s -1 -> X |
| 2072 | if (match(RHS, m_One())) |
| 2073 | return LHS; |
| 2074 | break; |
| 2075 | } |
| 2076 | } |
| 2077 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2078 | // If we are comparing with zero then try hard since this is a common case. |
| 2079 | if (match(RHS, m_Zero())) { |
| 2080 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 2081 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2082 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2083 | case ICmpInst::ICMP_ULT: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2084 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2085 | case ICmpInst::ICMP_UGE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2086 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2087 | case ICmpInst::ICMP_EQ: |
| 2088 | case ICmpInst::ICMP_ULE: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2089 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AT, Q.CxtI, Q.DT)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2090 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2091 | break; |
| 2092 | case ICmpInst::ICMP_NE: |
| 2093 | case ICmpInst::ICMP_UGT: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2094 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AT, Q.CxtI, Q.DT)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2095 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2096 | break; |
| 2097 | case ICmpInst::ICMP_SLT: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2098 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, |
| 2099 | 0, Q.AT, Q.CxtI, Q.DT); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2100 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2101 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2102 | if (LHSKnownNonNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2103 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2104 | break; |
| 2105 | case ICmpInst::ICMP_SLE: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2106 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, |
| 2107 | 0, Q.AT, Q.CxtI, Q.DT); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2108 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2109 | return getTrue(ITy); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2110 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, |
| 2111 | 0, Q.AT, Q.CxtI, Q.DT)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2112 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2113 | break; |
| 2114 | case ICmpInst::ICMP_SGE: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2115 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, |
| 2116 | 0, Q.AT, Q.CxtI, Q.DT); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2117 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2118 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2119 | if (LHSKnownNonNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2120 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2121 | break; |
| 2122 | case ICmpInst::ICMP_SGT: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2123 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, |
| 2124 | 0, Q.AT, Q.CxtI, Q.DT); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2125 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2126 | return getFalse(ITy); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2127 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, |
| 2128 | 0, Q.AT, Q.CxtI, Q.DT)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2129 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2130 | break; |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | // See if we are doing a comparison with a constant integer. |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2135 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2136 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
| 2137 | ConstantRange RHS_CR = ICmpInst::makeConstantRange(Pred, CI->getValue()); |
| 2138 | if (RHS_CR.isEmptySet()) |
| 2139 | return ConstantInt::getFalse(CI->getContext()); |
| 2140 | if (RHS_CR.isFullSet()) |
| 2141 | return ConstantInt::getTrue(CI->getContext()); |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2142 | |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2143 | // Many binary operators with constant RHS have easy to compute constant |
| 2144 | // range. Use them to check whether the comparison is a tautology. |
David Majnemer | 78910fc | 2014-05-16 17:14:03 +0000 | [diff] [blame] | 2145 | unsigned Width = CI->getBitWidth(); |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2146 | APInt Lower = APInt(Width, 0); |
| 2147 | APInt Upper = APInt(Width, 0); |
| 2148 | ConstantInt *CI2; |
| 2149 | if (match(LHS, m_URem(m_Value(), m_ConstantInt(CI2)))) { |
| 2150 | // 'urem x, CI2' produces [0, CI2). |
| 2151 | Upper = CI2->getValue(); |
| 2152 | } else if (match(LHS, m_SRem(m_Value(), m_ConstantInt(CI2)))) { |
| 2153 | // 'srem x, CI2' produces (-|CI2|, |CI2|). |
| 2154 | Upper = CI2->getValue().abs(); |
| 2155 | Lower = (-Upper) + 1; |
Duncan Sands | 92af0a8 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 2156 | } else if (match(LHS, m_UDiv(m_ConstantInt(CI2), m_Value()))) { |
| 2157 | // 'udiv CI2, x' produces [0, CI2]. |
Eli Friedman | 0bae8b2 | 2011-11-08 21:08:02 +0000 | [diff] [blame] | 2158 | Upper = CI2->getValue() + 1; |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2159 | } else if (match(LHS, m_UDiv(m_Value(), m_ConstantInt(CI2)))) { |
| 2160 | // 'udiv x, CI2' produces [0, UINT_MAX / CI2]. |
| 2161 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 2162 | if (!CI2->isZero()) |
| 2163 | Upper = NegOne.udiv(CI2->getValue()) + 1; |
David Majnemer | ea8d5db | 2014-05-16 16:57:04 +0000 | [diff] [blame] | 2164 | } else if (match(LHS, m_SDiv(m_ConstantInt(CI2), m_Value()))) { |
David Majnemer | 651ed5e | 2014-07-04 00:23:39 +0000 | [diff] [blame] | 2165 | if (CI2->isMinSignedValue()) { |
| 2166 | // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. |
| 2167 | Lower = CI2->getValue(); |
| 2168 | Upper = Lower.lshr(1) + 1; |
| 2169 | } else { |
| 2170 | // 'sdiv CI2, x' produces [-|CI2|, |CI2|]. |
| 2171 | Upper = CI2->getValue().abs() + 1; |
| 2172 | Lower = (-Upper) + 1; |
| 2173 | } |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2174 | } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) { |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2175 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2176 | APInt IntMax = APInt::getSignedMaxValue(Width); |
David Majnemer | af9180f | 2014-07-14 20:38:45 +0000 | [diff] [blame] | 2177 | APInt Val = CI2->getValue(); |
| 2178 | if (Val.isAllOnesValue()) { |
| 2179 | // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] |
| 2180 | // where CI2 != -1 and CI2 != 0 and CI2 != 1 |
| 2181 | Lower = IntMin + 1; |
| 2182 | Upper = IntMax + 1; |
| 2183 | } else if (Val.countLeadingZeros() < Width - 1) { |
| 2184 | // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2] |
| 2185 | // where CI2 != -1 and CI2 != 0 and CI2 != 1 |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2186 | Lower = IntMin.sdiv(Val); |
David Majnemer | af9180f | 2014-07-14 20:38:45 +0000 | [diff] [blame] | 2187 | Upper = IntMax.sdiv(Val); |
| 2188 | if (Lower.sgt(Upper)) |
| 2189 | std::swap(Lower, Upper); |
| 2190 | Upper = Upper + 1; |
David Majnemer | 5ea4fc0 | 2014-07-14 19:49:57 +0000 | [diff] [blame] | 2191 | assert(Upper != Lower && "Upper part of range has wrapped!"); |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2192 | } |
David Majnemer | d6d1671 | 2014-08-27 18:03:46 +0000 | [diff] [blame] | 2193 | } else if (match(LHS, m_NUWShl(m_ConstantInt(CI2), m_Value()))) { |
| 2194 | // 'shl nuw CI2, x' produces [CI2, CI2 << CLZ(CI2)] |
| 2195 | Lower = CI2->getValue(); |
| 2196 | Upper = Lower.shl(Lower.countLeadingZeros()) + 1; |
| 2197 | } else if (match(LHS, m_NSWShl(m_ConstantInt(CI2), m_Value()))) { |
| 2198 | if (CI2->isNegative()) { |
| 2199 | // 'shl nsw CI2, x' produces [CI2 << CLO(CI2)-1, CI2] |
| 2200 | unsigned ShiftAmount = CI2->getValue().countLeadingOnes() - 1; |
| 2201 | Lower = CI2->getValue().shl(ShiftAmount); |
| 2202 | Upper = CI2->getValue() + 1; |
| 2203 | } else { |
| 2204 | // 'shl nsw CI2, x' produces [CI2, CI2 << CLZ(CI2)-1] |
| 2205 | unsigned ShiftAmount = CI2->getValue().countLeadingZeros() - 1; |
| 2206 | Lower = CI2->getValue(); |
| 2207 | Upper = CI2->getValue().shl(ShiftAmount) + 1; |
| 2208 | } |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2209 | } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) { |
| 2210 | // 'lshr x, CI2' produces [0, UINT_MAX >> CI2]. |
| 2211 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 2212 | if (CI2->getValue().ult(Width)) |
| 2213 | Upper = NegOne.lshr(CI2->getValue()) + 1; |
David Majnemer | 78910fc | 2014-05-16 17:14:03 +0000 | [diff] [blame] | 2214 | } else if (match(LHS, m_LShr(m_ConstantInt(CI2), m_Value()))) { |
| 2215 | // 'lshr CI2, x' produces [CI2 >> (Width-1), CI2]. |
| 2216 | unsigned ShiftAmount = Width - 1; |
| 2217 | if (!CI2->isZero() && cast<BinaryOperator>(LHS)->isExact()) |
| 2218 | ShiftAmount = CI2->getValue().countTrailingZeros(); |
| 2219 | Lower = CI2->getValue().lshr(ShiftAmount); |
| 2220 | Upper = CI2->getValue() + 1; |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2221 | } else if (match(LHS, m_AShr(m_Value(), m_ConstantInt(CI2)))) { |
| 2222 | // 'ashr x, CI2' produces [INT_MIN >> CI2, INT_MAX >> CI2]. |
| 2223 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2224 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 2225 | if (CI2->getValue().ult(Width)) { |
| 2226 | Lower = IntMin.ashr(CI2->getValue()); |
| 2227 | Upper = IntMax.ashr(CI2->getValue()) + 1; |
| 2228 | } |
David Majnemer | 78910fc | 2014-05-16 17:14:03 +0000 | [diff] [blame] | 2229 | } else if (match(LHS, m_AShr(m_ConstantInt(CI2), m_Value()))) { |
| 2230 | unsigned ShiftAmount = Width - 1; |
| 2231 | if (!CI2->isZero() && cast<BinaryOperator>(LHS)->isExact()) |
| 2232 | ShiftAmount = CI2->getValue().countTrailingZeros(); |
| 2233 | if (CI2->isNegative()) { |
| 2234 | // 'ashr CI2, x' produces [CI2, CI2 >> (Width-1)] |
| 2235 | Lower = CI2->getValue(); |
| 2236 | Upper = CI2->getValue().ashr(ShiftAmount) + 1; |
| 2237 | } else { |
| 2238 | // 'ashr CI2, x' produces [CI2 >> (Width-1), CI2] |
| 2239 | Lower = CI2->getValue().ashr(ShiftAmount); |
| 2240 | Upper = CI2->getValue() + 1; |
| 2241 | } |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2242 | } else if (match(LHS, m_Or(m_Value(), m_ConstantInt(CI2)))) { |
| 2243 | // 'or x, CI2' produces [CI2, UINT_MAX]. |
| 2244 | Lower = CI2->getValue(); |
| 2245 | } else if (match(LHS, m_And(m_Value(), m_ConstantInt(CI2)))) { |
| 2246 | // 'and x, CI2' produces [0, CI2]. |
| 2247 | Upper = CI2->getValue() + 1; |
| 2248 | } |
| 2249 | if (Lower != Upper) { |
| 2250 | ConstantRange LHS_CR = ConstantRange(Lower, Upper); |
| 2251 | if (RHS_CR.contains(LHS_CR)) |
| 2252 | return ConstantInt::getTrue(RHS->getContext()); |
| 2253 | if (RHS_CR.inverse().contains(LHS_CR)) |
| 2254 | return ConstantInt::getFalse(RHS->getContext()); |
| 2255 | } |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2258 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 2259 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 2260 | Instruction *LI = cast<CastInst>(LHS); |
| 2261 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2262 | Type *SrcTy = SrcOp->getType(); |
| 2263 | Type *DstTy = LI->getType(); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2264 | |
| 2265 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 2266 | // if the integer type is the same size as the pointer type. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2267 | if (MaxRecurse && Q.DL && isa<PtrToIntInst>(LI) && |
| 2268 | Q.DL->getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) { |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2269 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2270 | // Transfer the cast to the constant. |
| 2271 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 2272 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2273 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2274 | return V; |
| 2275 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 2276 | if (RI->getOperand(0)->getType() == SrcTy) |
| 2277 | // Compare without the cast. |
| 2278 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2279 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2280 | return V; |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | if (isa<ZExtInst>(LHS)) { |
| 2285 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 2286 | // same type. |
| 2287 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 2288 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 2289 | // Compare X and Y. Note that signed predicates become unsigned. |
| 2290 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2291 | SrcOp, RI->getOperand(0), Q, |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2292 | MaxRecurse-1)) |
| 2293 | return V; |
| 2294 | } |
| 2295 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 2296 | // too. If not, then try to deduce the result of the comparison. |
| 2297 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2298 | // Compute the constant that would happen if we truncated to SrcTy then |
| 2299 | // reextended to DstTy. |
| 2300 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 2301 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 2302 | |
| 2303 | // If the re-extended constant didn't change then this is effectively |
| 2304 | // also a case of comparing two zero-extended values. |
| 2305 | if (RExt == CI && MaxRecurse) |
| 2306 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2307 | SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2308 | return V; |
| 2309 | |
| 2310 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 2311 | // there. Use this to work out the result of the comparison. |
| 2312 | if (RExt != CI) { |
| 2313 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2314 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2315 | // LHS <u RHS. |
| 2316 | case ICmpInst::ICMP_EQ: |
| 2317 | case ICmpInst::ICMP_UGT: |
| 2318 | case ICmpInst::ICMP_UGE: |
| 2319 | return ConstantInt::getFalse(CI->getContext()); |
| 2320 | |
| 2321 | case ICmpInst::ICMP_NE: |
| 2322 | case ICmpInst::ICMP_ULT: |
| 2323 | case ICmpInst::ICMP_ULE: |
| 2324 | return ConstantInt::getTrue(CI->getContext()); |
| 2325 | |
| 2326 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 2327 | // is non-negative then LHS <s RHS. |
| 2328 | case ICmpInst::ICMP_SGT: |
| 2329 | case ICmpInst::ICMP_SGE: |
| 2330 | return CI->getValue().isNegative() ? |
| 2331 | ConstantInt::getTrue(CI->getContext()) : |
| 2332 | ConstantInt::getFalse(CI->getContext()); |
| 2333 | |
| 2334 | case ICmpInst::ICMP_SLT: |
| 2335 | case ICmpInst::ICMP_SLE: |
| 2336 | return CI->getValue().isNegative() ? |
| 2337 | ConstantInt::getFalse(CI->getContext()) : |
| 2338 | ConstantInt::getTrue(CI->getContext()); |
| 2339 | } |
| 2340 | } |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | if (isa<SExtInst>(LHS)) { |
| 2345 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 2346 | // same type. |
| 2347 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 2348 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 2349 | // Compare X and Y. Note that the predicate does not change. |
| 2350 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2351 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2352 | return V; |
| 2353 | } |
| 2354 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 2355 | // too. If not, then try to deduce the result of the comparison. |
| 2356 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2357 | // Compute the constant that would happen if we truncated to SrcTy then |
| 2358 | // reextended to DstTy. |
| 2359 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 2360 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 2361 | |
| 2362 | // If the re-extended constant didn't change then this is effectively |
| 2363 | // also a case of comparing two sign-extended values. |
| 2364 | if (RExt == CI && MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2365 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2366 | return V; |
| 2367 | |
| 2368 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 2369 | // bits there. Use this to work out the result of the comparison. |
| 2370 | if (RExt != CI) { |
| 2371 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2372 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2373 | case ICmpInst::ICMP_EQ: |
| 2374 | return ConstantInt::getFalse(CI->getContext()); |
| 2375 | case ICmpInst::ICMP_NE: |
| 2376 | return ConstantInt::getTrue(CI->getContext()); |
| 2377 | |
| 2378 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 2379 | // LHS >s RHS. |
| 2380 | case ICmpInst::ICMP_SGT: |
| 2381 | case ICmpInst::ICMP_SGE: |
| 2382 | return CI->getValue().isNegative() ? |
| 2383 | ConstantInt::getTrue(CI->getContext()) : |
| 2384 | ConstantInt::getFalse(CI->getContext()); |
| 2385 | case ICmpInst::ICMP_SLT: |
| 2386 | case ICmpInst::ICMP_SLE: |
| 2387 | return CI->getValue().isNegative() ? |
| 2388 | ConstantInt::getFalse(CI->getContext()) : |
| 2389 | ConstantInt::getTrue(CI->getContext()); |
| 2390 | |
| 2391 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 2392 | // LHS >u RHS. |
| 2393 | case ICmpInst::ICMP_UGT: |
| 2394 | case ICmpInst::ICMP_UGE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2395 | // Comparison is true iff the LHS <s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2396 | if (MaxRecurse) |
| 2397 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 2398 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2399 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2400 | return V; |
| 2401 | break; |
| 2402 | case ICmpInst::ICMP_ULT: |
| 2403 | case ICmpInst::ICMP_ULE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2404 | // Comparison is true iff the LHS >=s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2405 | if (MaxRecurse) |
| 2406 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 2407 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2408 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2409 | return V; |
| 2410 | break; |
| 2411 | } |
| 2412 | } |
| 2413 | } |
| 2414 | } |
| 2415 | } |
| 2416 | |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2417 | // Special logic for binary operators. |
| 2418 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 2419 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 2420 | if (MaxRecurse && (LBO || RBO)) { |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2421 | // Analyze the case when either LHS or RHS is an add instruction. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2422 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2423 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 2424 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 2425 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 2426 | A = LBO->getOperand(0); B = LBO->getOperand(1); |
| 2427 | NoLHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 2428 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 2429 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 2430 | } |
| 2431 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 2432 | C = RBO->getOperand(0); D = RBO->getOperand(1); |
| 2433 | NoRHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 2434 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 2435 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 2436 | } |
| 2437 | |
| 2438 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2439 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 2440 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 2441 | Constant::getNullValue(RHS->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2442 | Q, MaxRecurse-1)) |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2443 | return V; |
| 2444 | |
| 2445 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2446 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 2447 | if (Value *V = SimplifyICmpInst(Pred, |
| 2448 | Constant::getNullValue(LHS->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2449 | C == LHS ? D : C, Q, MaxRecurse-1)) |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2450 | return V; |
| 2451 | |
| 2452 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 2453 | if (A && C && (A == C || A == D || B == C || B == D) && |
| 2454 | NoLHSWrapProblem && NoRHSWrapProblem) { |
| 2455 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2456 | Value *Y, *Z; |
| 2457 | if (A == C) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2458 | // C + B == C + D -> B == D |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2459 | Y = B; |
| 2460 | Z = D; |
| 2461 | } else if (A == D) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2462 | // D + B == C + D -> B == C |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2463 | Y = B; |
| 2464 | Z = C; |
| 2465 | } else if (B == C) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2466 | // A + C == C + D -> A == D |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2467 | Y = A; |
| 2468 | Z = D; |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2469 | } else { |
| 2470 | assert(B == D); |
| 2471 | // A + D == C + D -> A == C |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2472 | Y = A; |
| 2473 | Z = C; |
| 2474 | } |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2475 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2476 | return V; |
| 2477 | } |
| 2478 | } |
| 2479 | |
David Majnemer | bd9ce4e | 2014-11-25 02:55:48 +0000 | [diff] [blame] | 2480 | // icmp pred (or X, Y), X |
| 2481 | if (LBO && match(LBO, m_CombineOr(m_Or(m_Value(), m_Specific(RHS)), |
| 2482 | m_Or(m_Specific(RHS), m_Value())))) { |
| 2483 | if (Pred == ICmpInst::ICMP_ULT) |
| 2484 | return getFalse(ITy); |
| 2485 | if (Pred == ICmpInst::ICMP_UGE) |
| 2486 | return getTrue(ITy); |
| 2487 | } |
| 2488 | // icmp pred X, (or X, Y) |
| 2489 | if (RBO && match(RBO, m_CombineOr(m_Or(m_Value(), m_Specific(LHS)), |
| 2490 | m_Or(m_Specific(LHS), m_Value())))) { |
| 2491 | if (Pred == ICmpInst::ICMP_ULE) |
| 2492 | return getTrue(ITy); |
| 2493 | if (Pred == ICmpInst::ICMP_UGT) |
| 2494 | return getFalse(ITy); |
| 2495 | } |
| 2496 | |
| 2497 | // icmp pred (and X, Y), X |
| 2498 | if (LBO && match(LBO, m_CombineOr(m_And(m_Value(), m_Specific(RHS)), |
| 2499 | m_And(m_Specific(RHS), m_Value())))) { |
| 2500 | if (Pred == ICmpInst::ICMP_UGT) |
| 2501 | return getFalse(ITy); |
| 2502 | if (Pred == ICmpInst::ICMP_ULE) |
| 2503 | return getTrue(ITy); |
| 2504 | } |
| 2505 | // icmp pred X, (and X, Y) |
| 2506 | if (RBO && match(RBO, m_CombineOr(m_And(m_Value(), m_Specific(LHS)), |
| 2507 | m_And(m_Specific(LHS), m_Value())))) { |
| 2508 | if (Pred == ICmpInst::ICMP_UGE) |
| 2509 | return getTrue(ITy); |
| 2510 | if (Pred == ICmpInst::ICMP_ULT) |
| 2511 | return getFalse(ITy); |
| 2512 | } |
| 2513 | |
David Majnemer | 2d6c023 | 2014-05-14 20:16:28 +0000 | [diff] [blame] | 2514 | // 0 - (zext X) pred C |
| 2515 | if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) { |
| 2516 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2517 | if (RHSC->getValue().isStrictlyPositive()) { |
| 2518 | if (Pred == ICmpInst::ICMP_SLT) |
| 2519 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2520 | if (Pred == ICmpInst::ICMP_SGE) |
| 2521 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2522 | if (Pred == ICmpInst::ICMP_EQ) |
| 2523 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2524 | if (Pred == ICmpInst::ICMP_NE) |
| 2525 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2526 | } |
| 2527 | if (RHSC->getValue().isNonNegative()) { |
| 2528 | if (Pred == ICmpInst::ICMP_SLE) |
| 2529 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2530 | if (Pred == ICmpInst::ICMP_SGT) |
| 2531 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2532 | } |
| 2533 | } |
| 2534 | } |
| 2535 | |
Nick Lewycky | 35aeea9 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2536 | // icmp pred (urem X, Y), Y |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2537 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2538 | bool KnownNonNegative, KnownNegative; |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2539 | switch (Pred) { |
| 2540 | default: |
| 2541 | break; |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2542 | case ICmpInst::ICMP_SGT: |
| 2543 | case ICmpInst::ICMP_SGE: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2544 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, |
| 2545 | 0, Q.AT, Q.CxtI, Q.DT); |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2546 | if (!KnownNonNegative) |
| 2547 | break; |
| 2548 | // fall-through |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2549 | case ICmpInst::ICMP_EQ: |
| 2550 | case ICmpInst::ICMP_UGT: |
| 2551 | case ICmpInst::ICMP_UGE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2552 | return getFalse(ITy); |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2553 | case ICmpInst::ICMP_SLT: |
| 2554 | case ICmpInst::ICMP_SLE: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2555 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, |
| 2556 | 0, Q.AT, Q.CxtI, Q.DT); |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2557 | if (!KnownNonNegative) |
| 2558 | break; |
| 2559 | // fall-through |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2560 | case ICmpInst::ICMP_NE: |
| 2561 | case ICmpInst::ICMP_ULT: |
| 2562 | case ICmpInst::ICMP_ULE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2563 | return getTrue(ITy); |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2564 | } |
| 2565 | } |
Nick Lewycky | 35aeea9 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2566 | |
| 2567 | // icmp pred X, (urem Y, X) |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2568 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
| 2569 | bool KnownNonNegative, KnownNegative; |
| 2570 | switch (Pred) { |
| 2571 | default: |
| 2572 | break; |
| 2573 | case ICmpInst::ICMP_SGT: |
| 2574 | case ICmpInst::ICMP_SGE: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2575 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, |
| 2576 | 0, Q.AT, Q.CxtI, Q.DT); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2577 | if (!KnownNonNegative) |
| 2578 | break; |
| 2579 | // fall-through |
Nick Lewycky | 774647d | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2580 | case ICmpInst::ICMP_NE: |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2581 | case ICmpInst::ICMP_UGT: |
| 2582 | case ICmpInst::ICMP_UGE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2583 | return getTrue(ITy); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2584 | case ICmpInst::ICMP_SLT: |
| 2585 | case ICmpInst::ICMP_SLE: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2586 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, |
| 2587 | 0, Q.AT, Q.CxtI, Q.DT); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2588 | if (!KnownNonNegative) |
| 2589 | break; |
| 2590 | // fall-through |
Nick Lewycky | 774647d | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2591 | case ICmpInst::ICMP_EQ: |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2592 | case ICmpInst::ICMP_ULT: |
| 2593 | case ICmpInst::ICMP_ULE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2594 | return getFalse(ITy); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2595 | } |
| 2596 | } |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2597 | |
Duncan Sands | 92af0a8 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 2598 | // x udiv y <=u x. |
| 2599 | if (LBO && match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) { |
| 2600 | // icmp pred (X /u Y), X |
| 2601 | if (Pred == ICmpInst::ICMP_UGT) |
| 2602 | return getFalse(ITy); |
| 2603 | if (Pred == ICmpInst::ICMP_ULE) |
| 2604 | return getTrue(ITy); |
| 2605 | } |
| 2606 | |
David Majnemer | 76d06bc | 2014-08-28 03:34:28 +0000 | [diff] [blame] | 2607 | // handle: |
| 2608 | // CI2 << X == CI |
| 2609 | // CI2 << X != CI |
| 2610 | // |
| 2611 | // where CI2 is a power of 2 and CI isn't |
| 2612 | if (auto *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2613 | const APInt *CI2Val, *CIVal = &CI->getValue(); |
| 2614 | if (LBO && match(LBO, m_Shl(m_APInt(CI2Val), m_Value())) && |
| 2615 | CI2Val->isPowerOf2()) { |
| 2616 | if (!CIVal->isPowerOf2()) { |
| 2617 | // CI2 << X can equal zero in some circumstances, |
| 2618 | // this simplification is unsafe if CI is zero. |
| 2619 | // |
| 2620 | // We know it is safe if: |
| 2621 | // - The shift is nsw, we can't shift out the one bit. |
| 2622 | // - The shift is nuw, we can't shift out the one bit. |
| 2623 | // - CI2 is one |
| 2624 | // - CI isn't zero |
| 2625 | if (LBO->hasNoSignedWrap() || LBO->hasNoUnsignedWrap() || |
| 2626 | *CI2Val == 1 || !CI->isZero()) { |
| 2627 | if (Pred == ICmpInst::ICMP_EQ) |
| 2628 | return ConstantInt::getFalse(RHS->getContext()); |
| 2629 | if (Pred == ICmpInst::ICMP_NE) |
| 2630 | return ConstantInt::getTrue(RHS->getContext()); |
| 2631 | } |
| 2632 | } |
| 2633 | if (CIVal->isSignBit() && *CI2Val == 1) { |
| 2634 | if (Pred == ICmpInst::ICMP_UGT) |
| 2635 | return ConstantInt::getFalse(RHS->getContext()); |
| 2636 | if (Pred == ICmpInst::ICMP_ULE) |
| 2637 | return ConstantInt::getTrue(RHS->getContext()); |
| 2638 | } |
| 2639 | } |
| 2640 | } |
| 2641 | |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2642 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 2643 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 2644 | switch (LBO->getOpcode()) { |
| 2645 | default: break; |
| 2646 | case Instruction::UDiv: |
| 2647 | case Instruction::LShr: |
| 2648 | if (ICmpInst::isSigned(Pred)) |
| 2649 | break; |
| 2650 | // fall-through |
| 2651 | case Instruction::SDiv: |
| 2652 | case Instruction::AShr: |
Eli Friedman | 8a20e66 | 2011-05-05 21:59:18 +0000 | [diff] [blame] | 2653 | if (!LBO->isExact() || !RBO->isExact()) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2654 | break; |
| 2655 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2656 | RBO->getOperand(0), Q, MaxRecurse-1)) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2657 | return V; |
| 2658 | break; |
| 2659 | case Instruction::Shl: { |
Duncan Sands | 020c194 | 2011-08-04 10:02:21 +0000 | [diff] [blame] | 2660 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2661 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2662 | if (!NUW && !NSW) |
| 2663 | break; |
| 2664 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2665 | break; |
| 2666 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2667 | RBO->getOperand(0), Q, MaxRecurse-1)) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2668 | return V; |
| 2669 | break; |
| 2670 | } |
| 2671 | } |
| 2672 | } |
| 2673 | |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2674 | // Simplify comparisons involving max/min. |
| 2675 | Value *A, *B; |
| 2676 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2677 | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2678 | |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2679 | // Signed variants on "max(a,b)>=a -> true". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2680 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2681 | if (A != RHS) std::swap(A, B); // smax(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2682 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2683 | // We analyze this as smax(A, B) pred A. |
| 2684 | P = Pred; |
| 2685 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2686 | (A == LHS || B == LHS)) { |
| 2687 | if (A != LHS) std::swap(A, B); // A pred smax(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2688 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2689 | // We analyze this as smax(A, B) swapped-pred A. |
| 2690 | P = CmpInst::getSwappedPredicate(Pred); |
| 2691 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2692 | (A == RHS || B == RHS)) { |
| 2693 | if (A != RHS) std::swap(A, B); // smin(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2694 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2695 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2696 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2697 | P = CmpInst::getSwappedPredicate(Pred); |
| 2698 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2699 | (A == LHS || B == LHS)) { |
| 2700 | if (A != LHS) std::swap(A, B); // A pred smin(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2701 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2702 | // We analyze this as smax(-A, -B) pred -A. |
| 2703 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2704 | P = Pred; |
| 2705 | } |
| 2706 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2707 | // Cases correspond to "max(A, B) p A". |
| 2708 | switch (P) { |
| 2709 | default: |
| 2710 | break; |
| 2711 | case CmpInst::ICMP_EQ: |
| 2712 | case CmpInst::ICMP_SLE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2713 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2714 | // in the max/min; if so, we can just return that. |
| 2715 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2716 | return V; |
| 2717 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2718 | return V; |
| 2719 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2720 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2721 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2722 | return V; |
| 2723 | break; |
| 2724 | case CmpInst::ICMP_NE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2725 | case CmpInst::ICMP_SGT: { |
| 2726 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2727 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2728 | // tested in the max/min; if so, we can just return that. |
| 2729 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2730 | return V; |
| 2731 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2732 | return V; |
| 2733 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2734 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2735 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2736 | return V; |
| 2737 | break; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2738 | } |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2739 | case CmpInst::ICMP_SGE: |
| 2740 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2741 | return getTrue(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2742 | case CmpInst::ICMP_SLT: |
| 2743 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2744 | return getFalse(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2745 | } |
| 2746 | } |
| 2747 | |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2748 | // Unsigned variants on "max(a,b)>=a -> true". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2749 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2750 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2751 | if (A != RHS) std::swap(A, B); // umax(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2752 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2753 | // We analyze this as umax(A, B) pred A. |
| 2754 | P = Pred; |
| 2755 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2756 | (A == LHS || B == LHS)) { |
| 2757 | if (A != LHS) std::swap(A, B); // A pred umax(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2758 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2759 | // We analyze this as umax(A, B) swapped-pred A. |
| 2760 | P = CmpInst::getSwappedPredicate(Pred); |
| 2761 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2762 | (A == RHS || B == RHS)) { |
| 2763 | if (A != RHS) std::swap(A, B); // umin(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2764 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2765 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 2766 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2767 | P = CmpInst::getSwappedPredicate(Pred); |
| 2768 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2769 | (A == LHS || B == LHS)) { |
| 2770 | if (A != LHS) std::swap(A, B); // A pred umin(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2771 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2772 | // We analyze this as umax(-A, -B) pred -A. |
| 2773 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2774 | P = Pred; |
| 2775 | } |
| 2776 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2777 | // Cases correspond to "max(A, B) p A". |
| 2778 | switch (P) { |
| 2779 | default: |
| 2780 | break; |
| 2781 | case CmpInst::ICMP_EQ: |
| 2782 | case CmpInst::ICMP_ULE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2783 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2784 | // in the max/min; if so, we can just return that. |
| 2785 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2786 | return V; |
| 2787 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2788 | return V; |
| 2789 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2790 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2791 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2792 | return V; |
| 2793 | break; |
| 2794 | case CmpInst::ICMP_NE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2795 | case CmpInst::ICMP_UGT: { |
| 2796 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2797 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2798 | // tested in the max/min; if so, we can just return that. |
| 2799 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2800 | return V; |
| 2801 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2802 | return V; |
| 2803 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2804 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2805 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2806 | return V; |
| 2807 | break; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2808 | } |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2809 | case CmpInst::ICMP_UGE: |
| 2810 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2811 | return getTrue(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2812 | case CmpInst::ICMP_ULT: |
| 2813 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2814 | return getFalse(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2818 | // Variants on "max(x,y) >= min(x,z)". |
| 2819 | Value *C, *D; |
| 2820 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2821 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 2822 | (A == C || A == D || B == C || B == D)) { |
| 2823 | // max(x, ?) pred min(x, ?). |
| 2824 | if (Pred == CmpInst::ICMP_SGE) |
| 2825 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2826 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2827 | if (Pred == CmpInst::ICMP_SLT) |
| 2828 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2829 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2830 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2831 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 2832 | (A == C || A == D || B == C || B == D)) { |
| 2833 | // min(x, ?) pred max(x, ?). |
| 2834 | if (Pred == CmpInst::ICMP_SLE) |
| 2835 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2836 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2837 | if (Pred == CmpInst::ICMP_SGT) |
| 2838 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2839 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2840 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2841 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 2842 | (A == C || A == D || B == C || B == D)) { |
| 2843 | // max(x, ?) pred min(x, ?). |
| 2844 | if (Pred == CmpInst::ICMP_UGE) |
| 2845 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2846 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2847 | if (Pred == CmpInst::ICMP_ULT) |
| 2848 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2849 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2850 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2851 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 2852 | (A == C || A == D || B == C || B == D)) { |
| 2853 | // min(x, ?) pred max(x, ?). |
| 2854 | if (Pred == CmpInst::ICMP_ULE) |
| 2855 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2856 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2857 | if (Pred == CmpInst::ICMP_UGT) |
| 2858 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2859 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2862 | // Simplify comparisons of related pointers using a powerful, recursive |
| 2863 | // GEP-walk when we have target data available.. |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 2864 | if (LHS->getType()->isPointerTy()) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2865 | if (Constant *C = computePointerICmp(Q.DL, Q.TLI, Pred, LHS, RHS)) |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2866 | return C; |
| 2867 | |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 2868 | if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { |
| 2869 | if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { |
| 2870 | if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && |
| 2871 | GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && |
| 2872 | (ICmpInst::isEquality(Pred) || |
| 2873 | (GLHS->isInBounds() && GRHS->isInBounds() && |
| 2874 | Pred == ICmpInst::getSignedPredicate(Pred)))) { |
| 2875 | // The bases are equal and the indices are constant. Build a constant |
| 2876 | // expression GEP with the same indices and a null base pointer to see |
| 2877 | // what constant folding can make out of it. |
| 2878 | Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); |
| 2879 | SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); |
| 2880 | Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS); |
| 2881 | |
| 2882 | SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); |
| 2883 | Constant *NewRHS = ConstantExpr::getGetElementPtr(Null, IndicesRHS); |
| 2884 | return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); |
| 2885 | } |
| 2886 | } |
| 2887 | } |
| 2888 | |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 2889 | // If a bit is known to be zero for A and known to be one for B, |
| 2890 | // then A and B cannot be equal. |
| 2891 | if (ICmpInst::isEquality(Pred)) { |
| 2892 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2893 | uint32_t BitWidth = CI->getBitWidth(); |
| 2894 | APInt LHSKnownZero(BitWidth, 0); |
| 2895 | APInt LHSKnownOne(BitWidth, 0); |
| 2896 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, Q.DL, /*Depth=*/0, Q.AT, |
| 2897 | Q.CxtI, Q.DT); |
| 2898 | const APInt &RHSVal = CI->getValue(); |
| 2899 | if (((LHSKnownZero & RHSVal) != 0) || ((LHSKnownOne & ~RHSVal) != 0)) |
| 2900 | return Pred == ICmpInst::ICMP_EQ |
| 2901 | ? ConstantInt::getFalse(CI->getContext()) |
| 2902 | : ConstantInt::getTrue(CI->getContext()); |
| 2903 | } |
| 2904 | } |
| 2905 | |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2906 | // If the comparison is with the result of a select instruction, check whether |
| 2907 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2908 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2909 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2910 | return V; |
| 2911 | |
| 2912 | // If the comparison is with the result of a phi instruction, check whether |
| 2913 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2914 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2915 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 2916 | return V; |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2917 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2918 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2921 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2922 | const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2923 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2924 | const DominatorTree *DT, |
| 2925 | AssumptionTracker *AT, |
| 2926 | Instruction *CxtI) { |
| 2927 | return ::SimplifyICmpInst(Predicate, LHS, RHS, Query (DL, TLI, DT, AT, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2928 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2931 | /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can |
| 2932 | /// fold the result. If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2933 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2934 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2935 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 2936 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 2937 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2938 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2939 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2940 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2941 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2942 | // If we have a constant, make sure it is on the RHS. |
| 2943 | std::swap(LHS, RHS); |
| 2944 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 2945 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2946 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2947 | // Fold trivial predicates. |
| 2948 | if (Pred == FCmpInst::FCMP_FALSE) |
| 2949 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2950 | if (Pred == FCmpInst::FCMP_TRUE) |
| 2951 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2952 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2953 | if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef |
| 2954 | return UndefValue::get(GetCompareTy(LHS)); |
| 2955 | |
| 2956 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2957 | if (LHS == RHS) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2958 | if (CmpInst::isTrueWhenEqual(Pred)) |
| 2959 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2960 | if (CmpInst::isFalseWhenEqual(Pred)) |
| 2961 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2962 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2963 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2964 | // Handle fcmp with constant RHS |
| 2965 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2966 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 2967 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 2968 | if (CFP->getValueAPF().isNaN()) { |
| 2969 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
| 2970 | return ConstantInt::getFalse(CFP->getContext()); |
| 2971 | assert(FCmpInst::isUnordered(Pred) && |
| 2972 | "Comparison must be either ordered or unordered!"); |
| 2973 | // True if unordered. |
| 2974 | return ConstantInt::getTrue(CFP->getContext()); |
| 2975 | } |
Dan Gohman | 754e4a9 | 2010-02-22 04:06:03 +0000 | [diff] [blame] | 2976 | // Check whether the constant is an infinity. |
| 2977 | if (CFP->getValueAPF().isInfinity()) { |
| 2978 | if (CFP->getValueAPF().isNegative()) { |
| 2979 | switch (Pred) { |
| 2980 | case FCmpInst::FCMP_OLT: |
| 2981 | // No value is ordered and less than negative infinity. |
| 2982 | return ConstantInt::getFalse(CFP->getContext()); |
| 2983 | case FCmpInst::FCMP_UGE: |
| 2984 | // All values are unordered with or at least negative infinity. |
| 2985 | return ConstantInt::getTrue(CFP->getContext()); |
| 2986 | default: |
| 2987 | break; |
| 2988 | } |
| 2989 | } else { |
| 2990 | switch (Pred) { |
| 2991 | case FCmpInst::FCMP_OGT: |
| 2992 | // No value is ordered and greater than infinity. |
| 2993 | return ConstantInt::getFalse(CFP->getContext()); |
| 2994 | case FCmpInst::FCMP_ULE: |
| 2995 | // All values are unordered with and at most infinity. |
| 2996 | return ConstantInt::getTrue(CFP->getContext()); |
| 2997 | default: |
| 2998 | break; |
| 2999 | } |
| 3000 | } |
| 3001 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3002 | } |
| 3003 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3004 | |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3005 | // If the comparison is with the result of a select instruction, check whether |
| 3006 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3007 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3008 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3009 | return V; |
| 3010 | |
| 3011 | // If the comparison is with the result of a phi instruction, check whether |
| 3012 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3013 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3014 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3015 | return V; |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3016 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3017 | return nullptr; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3020 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3021 | const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3022 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3023 | const DominatorTree *DT, |
| 3024 | AssumptionTracker *AT, |
| 3025 | const Instruction *CxtI) { |
| 3026 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, Query (DL, TLI, DT, AT, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3027 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3030 | /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold |
| 3031 | /// the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3032 | static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal, |
| 3033 | Value *FalseVal, const Query &Q, |
| 3034 | unsigned MaxRecurse) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3035 | // select true, X, Y -> X |
| 3036 | // select false, X, Y -> Y |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3037 | if (Constant *CB = dyn_cast<Constant>(CondVal)) { |
| 3038 | if (CB->isAllOnesValue()) |
| 3039 | return TrueVal; |
| 3040 | if (CB->isNullValue()) |
| 3041 | return FalseVal; |
| 3042 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3043 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3044 | // select C, X, X -> X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3045 | if (TrueVal == FalseVal) |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3046 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3047 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3048 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 3049 | if (isa<Constant>(TrueVal)) |
| 3050 | return TrueVal; |
| 3051 | return FalseVal; |
| 3052 | } |
Dan Gohman | 54664ed | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 3053 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 3054 | return FalseVal; |
| 3055 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 3056 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3057 | |
David Majnemer | c6a5e1d | 2014-11-27 06:32:46 +0000 | [diff] [blame] | 3058 | if (const auto *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 3059 | Value *X; |
| 3060 | const APInt *Y; |
| 3061 | if (ICI->isEquality() && |
| 3062 | match(ICI->getOperand(0), m_And(m_Value(X), m_APInt(Y))) && |
| 3063 | match(ICI->getOperand(1), m_Zero())) { |
| 3064 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 3065 | const APInt *C; |
| 3066 | // (X & Y) == 0 ? X & ~Y : X --> X |
| 3067 | // (X & Y) != 0 ? X & ~Y : X --> X & ~Y |
| 3068 | if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3069 | *Y == ~*C) |
| 3070 | return Pred == ICmpInst::ICMP_EQ ? FalseVal : TrueVal; |
| 3071 | // (X & Y) == 0 ? X : X & ~Y --> X & ~Y |
| 3072 | // (X & Y) != 0 ? X : X & ~Y --> X |
| 3073 | if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3074 | *Y == ~*C) |
| 3075 | return Pred == ICmpInst::ICMP_EQ ? FalseVal : TrueVal; |
| 3076 | |
| 3077 | if (Y->isPowerOf2()) { |
| 3078 | // (X & Y) == 0 ? X | Y : X --> X | Y |
| 3079 | // (X & Y) != 0 ? X | Y : X --> X |
| 3080 | if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3081 | *Y == *C) |
| 3082 | return Pred == ICmpInst::ICMP_EQ ? TrueVal : FalseVal; |
| 3083 | // (X & Y) == 0 ? X : X | Y --> X |
| 3084 | // (X & Y) != 0 ? X : X | Y --> X | Y |
| 3085 | if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3086 | *Y == *C) |
| 3087 | return Pred == ICmpInst::ICMP_EQ ? TrueVal : FalseVal; |
| 3088 | } |
| 3089 | } |
| 3090 | } |
| 3091 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3092 | return nullptr; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3095 | Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3096 | const DataLayout *DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3097 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3098 | const DominatorTree *DT, |
| 3099 | AssumptionTracker *AT, |
| 3100 | const Instruction *CxtI) { |
| 3101 | return ::SimplifySelectInst(Cond, TrueVal, FalseVal, |
| 3102 | Query (DL, TLI, DT, AT, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3105 | /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can |
| 3106 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3107 | static Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const Query &Q, unsigned) { |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3108 | // The type of the GEP pointer operand. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3109 | PointerType *PtrTy = cast<PointerType>(Ops[0]->getType()->getScalarType()); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3110 | unsigned AS = PtrTy->getAddressSpace(); |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3112 | // getelementptr P -> P. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3113 | if (Ops.size() == 1) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3114 | return Ops[0]; |
| 3115 | |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3116 | // Compute the (pointer) type returned by the GEP instruction. |
| 3117 | Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1)); |
| 3118 | Type *GEPTy = PointerType::get(LastType, AS); |
| 3119 | if (VectorType *VT = dyn_cast<VectorType>(Ops[0]->getType())) |
| 3120 | GEPTy = VectorType::get(GEPTy, VT->getNumElements()); |
| 3121 | |
| 3122 | if (isa<UndefValue>(Ops[0])) |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3123 | return UndefValue::get(GEPTy); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3124 | |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3125 | if (Ops.size() == 2) { |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3126 | // getelementptr P, 0 -> P. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3127 | if (match(Ops[1], m_Zero())) |
| 3128 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3129 | |
| 3130 | Type *Ty = PtrTy->getElementType(); |
| 3131 | if (Q.DL && Ty->isSized()) { |
| 3132 | Value *P; |
| 3133 | uint64_t C; |
| 3134 | uint64_t TyAllocSize = Q.DL->getTypeAllocSize(Ty); |
| 3135 | // getelementptr P, N -> P if P points to a type of zero size. |
| 3136 | if (TyAllocSize == 0) |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3137 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3138 | |
| 3139 | // The following transforms are only safe if the ptrtoint cast |
| 3140 | // doesn't truncate the pointers. |
| 3141 | if (Ops[1]->getType()->getScalarSizeInBits() == |
| 3142 | Q.DL->getPointerSizeInBits(AS)) { |
| 3143 | auto PtrToIntOrZero = [GEPTy](Value *P) -> Value * { |
| 3144 | if (match(P, m_Zero())) |
| 3145 | return Constant::getNullValue(GEPTy); |
| 3146 | Value *Temp; |
| 3147 | if (match(P, m_PtrToInt(m_Value(Temp)))) |
David Majnemer | 11ca297 | 2014-08-27 20:08:34 +0000 | [diff] [blame] | 3148 | if (Temp->getType() == GEPTy) |
| 3149 | return Temp; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3150 | return nullptr; |
| 3151 | }; |
| 3152 | |
| 3153 | // getelementptr V, (sub P, V) -> P if P points to a type of size 1. |
| 3154 | if (TyAllocSize == 1 && |
| 3155 | match(Ops[1], m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))))) |
| 3156 | if (Value *R = PtrToIntOrZero(P)) |
| 3157 | return R; |
| 3158 | |
| 3159 | // getelementptr V, (ashr (sub P, V), C) -> Q |
| 3160 | // if P points to a type of size 1 << C. |
| 3161 | if (match(Ops[1], |
| 3162 | m_AShr(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3163 | m_ConstantInt(C))) && |
| 3164 | TyAllocSize == 1ULL << C) |
| 3165 | if (Value *R = PtrToIntOrZero(P)) |
| 3166 | return R; |
| 3167 | |
| 3168 | // getelementptr V, (sdiv (sub P, V), C) -> Q |
| 3169 | // if P points to a type of size C. |
| 3170 | if (match(Ops[1], |
| 3171 | m_SDiv(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3172 | m_SpecificInt(TyAllocSize)))) |
| 3173 | if (Value *R = PtrToIntOrZero(P)) |
| 3174 | return R; |
| 3175 | } |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3176 | } |
| 3177 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3178 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3179 | // Check to see if this is constant foldable. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3180 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3181 | if (!isa<Constant>(Ops[i])) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3182 | return nullptr; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3183 | |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 3184 | return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), Ops.slice(1)); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3187 | Value *llvm::SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3188 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3189 | const DominatorTree *DT, AssumptionTracker *AT, |
| 3190 | const Instruction *CxtI) { |
| 3191 | return ::SimplifyGEPInst(Ops, Query (DL, TLI, DT, AT, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3194 | /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we |
| 3195 | /// can fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3196 | static Value *SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 3197 | ArrayRef<unsigned> Idxs, const Query &Q, |
| 3198 | unsigned) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3199 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 3200 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 3201 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 3202 | |
| 3203 | // insertvalue x, undef, n -> x |
| 3204 | if (match(Val, m_Undef())) |
| 3205 | return Agg; |
| 3206 | |
| 3207 | // insertvalue x, (extractvalue y, n), n |
| 3208 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | 4b79c21 | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 3209 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 3210 | EV->getIndices() == Idxs) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3211 | // insertvalue undef, (extractvalue y, n), n -> y |
| 3212 | if (match(Agg, m_Undef())) |
| 3213 | return EV->getAggregateOperand(); |
| 3214 | |
| 3215 | // insertvalue y, (extractvalue y, n), n -> y |
| 3216 | if (Agg == EV->getAggregateOperand()) |
| 3217 | return Agg; |
| 3218 | } |
| 3219 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3220 | return nullptr; |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3223 | Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 3224 | ArrayRef<unsigned> Idxs, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3225 | const DataLayout *DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3226 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3227 | const DominatorTree *DT, |
| 3228 | AssumptionTracker *AT, |
| 3229 | const Instruction *CxtI) { |
| 3230 | return ::SimplifyInsertValueInst(Agg, Val, Idxs, |
| 3231 | Query (DL, TLI, DT, AT, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3232 | RecursionLimit); |
| 3233 | } |
| 3234 | |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3235 | /// SimplifyPHINode - See if we can fold the given phi. If not, returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3236 | static Value *SimplifyPHINode(PHINode *PN, const Query &Q) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3237 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 3238 | // with the common value. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3239 | Value *CommonValue = nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3240 | bool HasUndefInput = false; |
| 3241 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 3242 | Value *Incoming = PN->getIncomingValue(i); |
| 3243 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 3244 | if (Incoming == PN) continue; |
| 3245 | if (isa<UndefValue>(Incoming)) { |
| 3246 | // Remember that we saw an undef value, but otherwise ignore them. |
| 3247 | HasUndefInput = true; |
| 3248 | continue; |
| 3249 | } |
| 3250 | if (CommonValue && Incoming != CommonValue) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3251 | return nullptr; // Not the same, bail out. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3252 | CommonValue = Incoming; |
| 3253 | } |
| 3254 | |
| 3255 | // If CommonValue is null then all of the incoming values were either undef or |
| 3256 | // equal to the phi node itself. |
| 3257 | if (!CommonValue) |
| 3258 | return UndefValue::get(PN->getType()); |
| 3259 | |
| 3260 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 3261 | // instruction, we cannot return X as the result of the PHI node unless it |
| 3262 | // dominates the PHI block. |
| 3263 | if (HasUndefInput) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3264 | return ValueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3265 | |
| 3266 | return CommonValue; |
| 3267 | } |
| 3268 | |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3269 | static Value *SimplifyTruncInst(Value *Op, Type *Ty, const Query &Q, unsigned) { |
| 3270 | if (Constant *C = dyn_cast<Constant>(Op)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3271 | return ConstantFoldInstOperands(Instruction::Trunc, Ty, C, Q.DL, Q.TLI); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3272 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3273 | return nullptr; |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3276 | Value *llvm::SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *DL, |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3277 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3278 | const DominatorTree *DT, |
| 3279 | AssumptionTracker *AT, |
| 3280 | const Instruction *CxtI) { |
| 3281 | return ::SimplifyTruncInst(Op, Ty, Query (DL, TLI, DT, AT, CxtI), |
| 3282 | RecursionLimit); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3285 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3286 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3287 | /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can |
| 3288 | /// fold the result. If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3289 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3290 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3291 | switch (Opcode) { |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3292 | case Instruction::Add: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 3293 | return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3294 | Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 3295 | case Instruction::FAdd: |
| 3296 | return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 3297 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3298 | case Instruction::Sub: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 3299 | return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3300 | Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 3301 | case Instruction::FSub: |
| 3302 | return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 3303 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3304 | case Instruction::Mul: return SimplifyMulInst (LHS, RHS, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 3305 | case Instruction::FMul: |
| 3306 | return SimplifyFMulInst (LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3307 | case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, Q, MaxRecurse); |
| 3308 | case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse); |
| 3309 | case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, Q, MaxRecurse); |
| 3310 | case Instruction::SRem: return SimplifySRemInst(LHS, RHS, Q, MaxRecurse); |
| 3311 | case Instruction::URem: return SimplifyURemInst(LHS, RHS, Q, MaxRecurse); |
| 3312 | case Instruction::FRem: return SimplifyFRemInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3313 | case Instruction::Shl: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 3314 | return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3315 | Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3316 | case Instruction::LShr: |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3317 | return SimplifyLShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3318 | case Instruction::AShr: |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3319 | return SimplifyAShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
| 3320 | case Instruction::And: return SimplifyAndInst(LHS, RHS, Q, MaxRecurse); |
| 3321 | case Instruction::Or: return SimplifyOrInst (LHS, RHS, Q, MaxRecurse); |
| 3322 | case Instruction::Xor: return SimplifyXorInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3323 | default: |
| 3324 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 3325 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 3326 | Constant *COps[] = {CLHS, CRHS}; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3327 | return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, Q.DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3328 | Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3329 | } |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 3330 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 3331 | // If the operation is associative, try some generic simplifications. |
| 3332 | if (Instruction::isAssociative(Opcode)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3333 | if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 3334 | return V; |
| 3335 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3336 | // If the operation is with the result of a select instruction check whether |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 3337 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3338 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3339 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3340 | return V; |
| 3341 | |
| 3342 | // If the operation is with the result of a phi instruction, check whether |
| 3343 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3344 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3345 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 3346 | return V; |
| 3347 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3348 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3349 | } |
| 3350 | } |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3351 | |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3352 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3353 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3354 | const DominatorTree *DT, AssumptionTracker *AT, |
| 3355 | const Instruction *CxtI) { |
| 3356 | return ::SimplifyBinOp(Opcode, LHS, RHS, Query (DL, TLI, DT, AT, CxtI), |
| 3357 | RecursionLimit); |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3360 | /// SimplifyCmpInst - Given operands for a CmpInst, see if we can |
| 3361 | /// fold the result. |
| 3362 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3363 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3364 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3365 | return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
| 3366 | return SimplifyFCmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3370 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3371 | const DominatorTree *DT, AssumptionTracker *AT, |
| 3372 | const Instruction *CxtI) { |
| 3373 | return ::SimplifyCmpInst(Predicate, LHS, RHS, Query (DL, TLI, DT, AT, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3374 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3375 | } |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3376 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3377 | static bool IsIdempotent(Intrinsic::ID ID) { |
| 3378 | switch (ID) { |
| 3379 | default: return false; |
| 3380 | |
| 3381 | // Unary idempotent: f(f(x)) = f(x) |
| 3382 | case Intrinsic::fabs: |
| 3383 | case Intrinsic::floor: |
| 3384 | case Intrinsic::ceil: |
| 3385 | case Intrinsic::trunc: |
| 3386 | case Intrinsic::rint: |
| 3387 | case Intrinsic::nearbyint: |
Hal Finkel | 171817e | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 3388 | case Intrinsic::round: |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3389 | return true; |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | template <typename IterTy> |
| 3394 | static Value *SimplifyIntrinsic(Intrinsic::ID IID, IterTy ArgBegin, IterTy ArgEnd, |
| 3395 | const Query &Q, unsigned MaxRecurse) { |
| 3396 | // Perform idempotent optimizations |
| 3397 | if (!IsIdempotent(IID)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3398 | return nullptr; |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3399 | |
| 3400 | // Unary Ops |
| 3401 | if (std::distance(ArgBegin, ArgEnd) == 1) |
| 3402 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin)) |
| 3403 | if (II->getIntrinsicID() == IID) |
| 3404 | return II; |
| 3405 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3406 | return nullptr; |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3409 | template <typename IterTy> |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3410 | static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd, |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3411 | const Query &Q, unsigned MaxRecurse) { |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3412 | Type *Ty = V->getType(); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3413 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) |
| 3414 | Ty = PTy->getElementType(); |
| 3415 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 3416 | |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3417 | // call undef -> undef |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3418 | if (isa<UndefValue>(V)) |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3419 | return UndefValue::get(FTy->getReturnType()); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3420 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3421 | Function *F = dyn_cast<Function>(V); |
| 3422 | if (!F) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3423 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3424 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3425 | if (unsigned IID = F->getIntrinsicID()) |
| 3426 | if (Value *Ret = |
| 3427 | SimplifyIntrinsic((Intrinsic::ID) IID, ArgBegin, ArgEnd, Q, MaxRecurse)) |
| 3428 | return Ret; |
| 3429 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3430 | if (!canConstantFoldCallTo(F)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3431 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3432 | |
| 3433 | SmallVector<Constant *, 4> ConstantArgs; |
| 3434 | ConstantArgs.reserve(ArgEnd - ArgBegin); |
| 3435 | for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) { |
| 3436 | Constant *C = dyn_cast<Constant>(*I); |
| 3437 | if (!C) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3438 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3439 | ConstantArgs.push_back(C); |
| 3440 | } |
| 3441 | |
| 3442 | return ConstantFoldCall(F, ConstantArgs, Q.TLI); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3445 | Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3446 | User::op_iterator ArgEnd, const DataLayout *DL, |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3447 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3448 | const DominatorTree *DT, AssumptionTracker *AT, |
| 3449 | const Instruction *CxtI) { |
| 3450 | return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(DL, TLI, DT, AT, CxtI), |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3451 | RecursionLimit); |
| 3452 | } |
| 3453 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3454 | Value *llvm::SimplifyCall(Value *V, ArrayRef<Value *> Args, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3455 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3456 | const DominatorTree *DT, AssumptionTracker *AT, |
| 3457 | const Instruction *CxtI) { |
| 3458 | return ::SimplifyCall(V, Args.begin(), Args.end(), |
| 3459 | Query(DL, TLI, DT, AT, CxtI), RecursionLimit); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3462 | /// SimplifyInstruction - See if we can compute a simplified version of this |
| 3463 | /// instruction. If not, this returns null. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3464 | Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3465 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3466 | const DominatorTree *DT, |
| 3467 | AssumptionTracker *AT) { |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3468 | Value *Result; |
| 3469 | |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3470 | switch (I->getOpcode()) { |
| 3471 | default: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3472 | Result = ConstantFoldInstruction(I, DL, TLI); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3473 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3474 | case Instruction::FAdd: |
| 3475 | Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3476 | I->getFastMathFlags(), DL, TLI, DT, AT, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3477 | break; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 3478 | case Instruction::Add: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3479 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 3480 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 3481 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3482 | DL, TLI, DT, AT, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3483 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3484 | case Instruction::FSub: |
| 3485 | Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3486 | I->getFastMathFlags(), DL, TLI, DT, AT, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3487 | break; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 3488 | case Instruction::Sub: |
| 3489 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 3490 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 3491 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3492 | DL, TLI, DT, AT, I); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 3493 | break; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 3494 | case Instruction::FMul: |
| 3495 | Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3496 | I->getFastMathFlags(), DL, TLI, DT, AT, I); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 3497 | break; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 3498 | case Instruction::Mul: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3499 | Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), |
| 3500 | DL, TLI, DT, AT, I); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 3501 | break; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 3502 | case Instruction::SDiv: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3503 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), |
| 3504 | DL, TLI, DT, AT, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 3505 | break; |
| 3506 | case Instruction::UDiv: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3507 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), |
| 3508 | DL, TLI, DT, AT, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 3509 | break; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 3510 | case Instruction::FDiv: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3511 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), |
| 3512 | DL, TLI, DT, AT, I); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 3513 | break; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3514 | case Instruction::SRem: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3515 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), |
| 3516 | DL, TLI, DT, AT, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3517 | break; |
| 3518 | case Instruction::URem: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3519 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), |
| 3520 | DL, TLI, DT, AT, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3521 | break; |
| 3522 | case Instruction::FRem: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3523 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), |
| 3524 | DL, TLI, DT, AT, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3525 | break; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3526 | case Instruction::Shl: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3527 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 3528 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 3529 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3530 | DL, TLI, DT, AT, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3531 | break; |
| 3532 | case Instruction::LShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3533 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
| 3534 | cast<BinaryOperator>(I)->isExact(), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3535 | DL, TLI, DT, AT, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3536 | break; |
| 3537 | case Instruction::AShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3538 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
| 3539 | cast<BinaryOperator>(I)->isExact(), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3540 | DL, TLI, DT, AT, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3541 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3542 | case Instruction::And: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3543 | Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), |
| 3544 | DL, TLI, DT, AT, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3545 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3546 | case Instruction::Or: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3547 | Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
| 3548 | AT, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3549 | break; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 3550 | case Instruction::Xor: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3551 | Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), |
| 3552 | DL, TLI, DT, AT, I); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 3553 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3554 | case Instruction::ICmp: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3555 | Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3556 | I->getOperand(0), I->getOperand(1), |
| 3557 | DL, TLI, DT, AT, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3558 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3559 | case Instruction::FCmp: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3560 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3561 | I->getOperand(0), I->getOperand(1), |
| 3562 | DL, TLI, DT, AT, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3563 | break; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3564 | case Instruction::Select: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3565 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3566 | I->getOperand(2), DL, TLI, DT, AT, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3567 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3568 | case Instruction::GetElementPtr: { |
| 3569 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3570 | Result = SimplifyGEPInst(Ops, DL, TLI, DT, AT, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3571 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3572 | } |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3573 | case Instruction::InsertValue: { |
| 3574 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 3575 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 3576 | IV->getInsertedValueOperand(), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3577 | IV->getIndices(), DL, TLI, DT, AT, I); |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3578 | break; |
| 3579 | } |
Duncan Sands | 4581ddc | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 3580 | case Instruction::PHI: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3581 | Result = SimplifyPHINode(cast<PHINode>(I), Query (DL, TLI, DT, AT, I)); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3582 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3583 | case Instruction::Call: { |
| 3584 | CallSite CS(cast<CallInst>(I)); |
| 3585 | Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3586 | DL, TLI, DT, AT, I); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3587 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3588 | } |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3589 | case Instruction::Trunc: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3590 | Result = SimplifyTruncInst(I->getOperand(0), I->getType(), DL, TLI, DT, |
| 3591 | AT, I); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3592 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3593 | } |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3594 | |
| 3595 | /// If called on unreachable code, the above logic may report that the |
| 3596 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 3597 | /// detecting that case here, returning a safe value instead. |
| 3598 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3601 | /// \brief Implementation of recursive simplification through an instructions |
| 3602 | /// uses. |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 3603 | /// |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3604 | /// This is the common implementation of the recursive simplification routines. |
| 3605 | /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to |
| 3606 | /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of |
| 3607 | /// instructions to process and attempt to simplify it using |
| 3608 | /// InstructionSimplify. |
| 3609 | /// |
| 3610 | /// This routine returns 'true' only when *it* simplifies something. The passed |
| 3611 | /// in simplified value does not count toward this. |
| 3612 | static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3613 | const DataLayout *DL, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3614 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3615 | const DominatorTree *DT, |
| 3616 | AssumptionTracker *AT) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3617 | bool Simplified = false; |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3618 | SmallSetVector<Instruction *, 8> Worklist; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3619 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3620 | // If we have an explicit value to collapse to, do that round of the |
| 3621 | // simplification loop by hand initially. |
| 3622 | if (SimpleV) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3623 | for (User *U : I->users()) |
| 3624 | if (U != I) |
| 3625 | Worklist.insert(cast<Instruction>(U)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3626 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3627 | // Replace the instruction with its simplified value. |
| 3628 | I->replaceAllUsesWith(SimpleV); |
Chris Lattner | 19eff2a | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 3629 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3630 | // Gracefully handle edge cases where the instruction is not wired into any |
| 3631 | // parent block. |
| 3632 | if (I->getParent()) |
| 3633 | I->eraseFromParent(); |
| 3634 | } else { |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3635 | Worklist.insert(I); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 3636 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3637 | |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3638 | // Note that we must test the size on each iteration, the worklist can grow. |
| 3639 | for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { |
| 3640 | I = Worklist[Idx]; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3641 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3642 | // See if this instruction simplifies. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3643 | SimpleV = SimplifyInstruction(I, DL, TLI, DT, AT); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3644 | if (!SimpleV) |
| 3645 | continue; |
| 3646 | |
| 3647 | Simplified = true; |
| 3648 | |
| 3649 | // Stash away all the uses of the old instruction so we can check them for |
| 3650 | // recursive simplifications after a RAUW. This is cheaper than checking all |
| 3651 | // uses of To on the recursive step in most cases. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3652 | for (User *U : I->users()) |
| 3653 | Worklist.insert(cast<Instruction>(U)); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3654 | |
| 3655 | // Replace the instruction with its simplified value. |
| 3656 | I->replaceAllUsesWith(SimpleV); |
| 3657 | |
| 3658 | // Gracefully handle edge cases where the instruction is not wired into any |
| 3659 | // parent block. |
| 3660 | if (I->getParent()) |
| 3661 | I->eraseFromParent(); |
| 3662 | } |
| 3663 | return Simplified; |
| 3664 | } |
| 3665 | |
| 3666 | bool llvm::recursivelySimplifyInstruction(Instruction *I, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3667 | const DataLayout *DL, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3668 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3669 | const DominatorTree *DT, |
| 3670 | AssumptionTracker *AT) { |
| 3671 | return replaceAndRecursivelySimplifyImpl(I, nullptr, DL, TLI, DT, AT); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3675 | const DataLayout *DL, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3676 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3677 | const DominatorTree *DT, |
| 3678 | AssumptionTracker *AT) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3679 | assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!"); |
| 3680 | assert(SimpleV && "Must provide a simplified value."); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3681 | return replaceAndRecursivelySimplifyImpl(I, SimpleV, DL, TLI, DT, AT); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 3682 | } |