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" |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/CaptureTracking.h" |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ConstantFolding.h" |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/MemoryBuiltins.h" |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/ValueTracking.h" |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/VectorUtils.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 30 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 33 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/GlobalAlias.h" |
| 35 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 36 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 37 | #include "llvm/IR/ValueHandle.h" |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 40 | using namespace llvm::PatternMatch; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 41 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "instsimplify" |
| 43 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 44 | enum { RecursionLimit = 3 }; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 45 | |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 46 | STATISTIC(NumExpand, "Number of expansions"); |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 47 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 48 | |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 49 | namespace { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 50 | struct Query { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 51 | const DataLayout &DL; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 52 | const TargetLibraryInfo *TLI; |
| 53 | const DominatorTree *DT; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 54 | AssumptionCache *AC; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 55 | const Instruction *CxtI; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 56 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 57 | Query(const DataLayout &DL, const TargetLibraryInfo *tli, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 58 | const DominatorTree *dt, AssumptionCache *ac = nullptr, |
| 59 | const Instruction *cxti = nullptr) |
| 60 | : DL(DL), TLI(tli), DT(dt), AC(ac), CxtI(cxti) {} |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 61 | }; |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 62 | } // end anonymous namespace |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 63 | |
| 64 | static Value *SimplifyAndInst(Value *, Value *, const Query &, unsigned); |
| 65 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 66 | unsigned); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 67 | static Value *SimplifyFPBinOp(unsigned, Value *, Value *, const FastMathFlags &, |
| 68 | const Query &, unsigned); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 69 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 70 | unsigned); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 71 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
| 72 | const Query &Q, unsigned MaxRecurse); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 73 | static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned); |
| 74 | static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned); |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 75 | static Value *SimplifyCastInst(unsigned, Value *, Type *, |
| 76 | const Query &, unsigned); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 77 | |
Sanjay Patel | 35ed241 | 2017-04-16 17:43:11 +0000 | [diff] [blame] | 78 | /// For a boolean type or a vector of boolean type, return false or a vector |
| 79 | /// with every element false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 80 | static Constant *getFalse(Type *Ty) { |
Sanjay Patel | 35ed241 | 2017-04-16 17:43:11 +0000 | [diff] [blame] | 81 | return ConstantInt::getFalse(Ty); |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Sanjay Patel | 35ed241 | 2017-04-16 17:43:11 +0000 | [diff] [blame] | 84 | /// For a boolean type or a vector of boolean type, return true or a vector |
| 85 | /// with every element true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 86 | static Constant *getTrue(Type *Ty) { |
Sanjay Patel | 35ed241 | 2017-04-16 17:43:11 +0000 | [diff] [blame] | 87 | return ConstantInt::getTrue(Ty); |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 90 | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
| 91 | static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, |
| 92 | Value *RHS) { |
| 93 | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
| 94 | if (!Cmp) |
| 95 | return false; |
| 96 | CmpInst::Predicate CPred = Cmp->getPredicate(); |
| 97 | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
| 98 | if (CPred == Pred && CLHS == LHS && CRHS == RHS) |
| 99 | return true; |
| 100 | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && |
| 101 | CRHS == LHS; |
| 102 | } |
| 103 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 104 | /// Does the given value dominate the specified phi node? |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 105 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 106 | Instruction *I = dyn_cast<Instruction>(V); |
| 107 | if (!I) |
| 108 | // Arguments and constants dominate all instructions. |
| 109 | return true; |
| 110 | |
Chandler Carruth | 3ffccb3 | 2012-03-21 10:58:47 +0000 | [diff] [blame] | 111 | // If we are processing instructions (and/or basic blocks) that have not been |
| 112 | // fully added to a function, the parent nodes may still be null. Simply |
| 113 | // return the conservative answer in these cases. |
| 114 | if (!I->getParent() || !P->getParent() || !I->getParent()->getParent()) |
| 115 | return false; |
| 116 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 117 | // If we have a DominatorTree then do a precise test. |
Eli Friedman | c8cbd06 | 2012-03-13 01:06:07 +0000 | [diff] [blame] | 118 | if (DT) { |
| 119 | if (!DT->isReachableFromEntry(P->getParent())) |
| 120 | return true; |
| 121 | if (!DT->isReachableFromEntry(I->getParent())) |
| 122 | return false; |
| 123 | return DT->dominates(I, P); |
| 124 | } |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 125 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 126 | // Otherwise, if the instruction is in the entry block and is not an invoke, |
| 127 | // then it obviously dominates all phi nodes. |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 128 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 129 | !isa<InvokeInst>(I)) |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 130 | return true; |
| 131 | |
| 132 | return false; |
| 133 | } |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 134 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 135 | /// Simplify "A op (B op' C)" by distributing op over op', turning it into |
| 136 | /// "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 137 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 138 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 139 | /// Returns the simplified value, or null if no simplification was performed. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 140 | static Value *ExpandBinOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, |
| 141 | Instruction::BinaryOps OpcodeToExpand, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 142 | unsigned MaxRecurse) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 143 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 144 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 145 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 146 | |
| 147 | // Check whether the expression has the form "(A op' B) op C". |
| 148 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 149 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 150 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 151 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 152 | // Do "A op C" and "B op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 153 | if (Value *L = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) |
| 154 | if (Value *R = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 155 | // They do! Return "L op' R" if it simplifies or is already available. |
| 156 | // 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] | 157 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 158 | && L == B && R == A)) { |
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 LHS; |
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 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 163 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 164 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 165 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 166 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | // Check whether the expression has the form "A op (B op' C)". |
| 171 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 172 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 173 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 174 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 175 | // Do "A op B" and "A op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 176 | if (Value *L = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) |
| 177 | if (Value *R = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 178 | // They do! Return "L op' R" if it simplifies or is already available. |
| 179 | // 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] | 180 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 181 | && L == C && R == B)) { |
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 RHS; |
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 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 186 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 187 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 188 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 189 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 193 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 196 | /// Generic simplifications for associative binary operations. |
| 197 | /// Returns the simpler value, or null if none was found. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 198 | static Value *SimplifyAssociativeBinOp(Instruction::BinaryOps Opcode, |
| 199 | Value *LHS, Value *RHS, const Query &Q, |
| 200 | unsigned MaxRecurse) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 201 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 202 | |
| 203 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 204 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 205 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 206 | |
| 207 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 208 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 209 | |
| 210 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 211 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 212 | Value *A = Op0->getOperand(0); |
| 213 | Value *B = Op0->getOperand(1); |
| 214 | Value *C = RHS; |
| 215 | |
| 216 | // Does "B op C" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 217 | if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 218 | // It does! Return "A op V" if it simplifies or is already available. |
| 219 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 220 | if (V == B) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 221 | // Otherwise return "A op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 222 | if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 223 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 224 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 225 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 230 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 231 | Value *A = LHS; |
| 232 | Value *B = Op1->getOperand(0); |
| 233 | Value *C = Op1->getOperand(1); |
| 234 | |
| 235 | // Does "A op B" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 236 | if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 237 | // It does! Return "V op C" if it simplifies or is already available. |
| 238 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 239 | if (V == B) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 240 | // Otherwise return "V op C" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 241 | if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 242 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 243 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 244 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | // The remaining transforms require commutativity as well as associativity. |
| 249 | if (!Instruction::isCommutative(Opcode)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 250 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 251 | |
| 252 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 253 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 254 | Value *A = Op0->getOperand(0); |
| 255 | Value *B = Op0->getOperand(1); |
| 256 | Value *C = RHS; |
| 257 | |
| 258 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 259 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 260 | // It does! Return "V op B" if it simplifies or is already available. |
| 261 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 262 | if (V == A) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 263 | // Otherwise return "V op B" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 264 | if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 265 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 266 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 267 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 272 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 273 | Value *A = LHS; |
| 274 | Value *B = Op1->getOperand(0); |
| 275 | Value *C = Op1->getOperand(1); |
| 276 | |
| 277 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 278 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 279 | // It does! Return "B op V" if it simplifies or is already available. |
| 280 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 281 | if (V == C) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 282 | // Otherwise return "B op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 283 | if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 284 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 285 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 286 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 290 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 293 | /// In the case of a binary operation with a select instruction as an operand, |
| 294 | /// try to simplify the binop by seeing whether evaluating it on both branches |
| 295 | /// of the select results in the same value. Returns the common value if so, |
| 296 | /// otherwise returns null. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 297 | static Value *ThreadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS, |
| 298 | Value *RHS, const Query &Q, |
| 299 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 300 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 301 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 302 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 303 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 304 | SelectInst *SI; |
| 305 | if (isa<SelectInst>(LHS)) { |
| 306 | SI = cast<SelectInst>(LHS); |
| 307 | } else { |
| 308 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 309 | SI = cast<SelectInst>(RHS); |
| 310 | } |
| 311 | |
| 312 | // Evaluate the BinOp on the true and false branches of the select. |
| 313 | Value *TV; |
| 314 | Value *FV; |
| 315 | if (SI == LHS) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 316 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse); |
| 317 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 318 | } else { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 319 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse); |
| 320 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Duncan Sands | e3c5395 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 323 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 324 | // If they both failed to simplify then return null. |
| 325 | if (TV == FV) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 326 | return TV; |
| 327 | |
| 328 | // If one branch simplified to undef, return the other one. |
| 329 | if (TV && isa<UndefValue>(TV)) |
| 330 | return FV; |
| 331 | if (FV && isa<UndefValue>(FV)) |
| 332 | return TV; |
| 333 | |
| 334 | // If applying the operation did not change the true and false select values, |
| 335 | // then the result of the binop is the select itself. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 336 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 337 | return SI; |
| 338 | |
| 339 | // If one branch simplified and the other did not, and the simplified |
| 340 | // value is equal to the unsimplified one, return the simplified value. |
| 341 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 342 | if ((FV && !TV) || (TV && !FV)) { |
| 343 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 344 | // same as the original operation. |
| 345 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 346 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 347 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 348 | // We already know that "op" is the same as for the simplified value. See |
| 349 | // if the operands match too. If so, return the simplified value. |
| 350 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 351 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 352 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 353 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 354 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 355 | return Simplified; |
| 356 | if (Simplified->isCommutative() && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 357 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 358 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 359 | return Simplified; |
| 360 | } |
| 361 | } |
| 362 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 363 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 366 | /// In the case of a comparison with a select instruction, try to simplify the |
| 367 | /// comparison by seeing whether both branches of the select result in the same |
| 368 | /// value. Returns the common value if so, otherwise returns null. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 369 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 370 | Value *RHS, const Query &Q, |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 371 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 372 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 373 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 374 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 375 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 376 | // Make sure the select is on the LHS. |
| 377 | if (!isa<SelectInst>(LHS)) { |
| 378 | std::swap(LHS, RHS); |
| 379 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 380 | } |
| 381 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 382 | SelectInst *SI = cast<SelectInst>(LHS); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 383 | Value *Cond = SI->getCondition(); |
| 384 | Value *TV = SI->getTrueValue(); |
| 385 | Value *FV = SI->getFalseValue(); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 386 | |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 387 | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 388 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 389 | Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 390 | if (TCmp == Cond) { |
| 391 | // It not only simplified, it simplified to the select condition. Replace |
| 392 | // it with 'true'. |
| 393 | TCmp = getTrue(Cond->getType()); |
| 394 | } else if (!TCmp) { |
| 395 | // It didn't simplify. However if "cmp TV, RHS" is equal to the select |
| 396 | // condition then we can replace it with 'true'. Otherwise give up. |
| 397 | if (!isSameCompare(Cond, Pred, TV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 398 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 399 | TCmp = getTrue(Cond->getType()); |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 402 | // Does "cmp FV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 403 | Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 404 | if (FCmp == Cond) { |
| 405 | // It not only simplified, it simplified to the select condition. Replace |
| 406 | // it with 'false'. |
| 407 | FCmp = getFalse(Cond->getType()); |
| 408 | } else if (!FCmp) { |
| 409 | // It didn't simplify. However if "cmp FV, RHS" is equal to the select |
| 410 | // condition then we can replace it with 'false'. Otherwise give up. |
| 411 | if (!isSameCompare(Cond, Pred, FV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 412 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 413 | FCmp = getFalse(Cond->getType()); |
| 414 | } |
| 415 | |
| 416 | // If both sides simplified to the same value, then use it as the result of |
| 417 | // the original comparison. |
| 418 | if (TCmp == FCmp) |
| 419 | return TCmp; |
Duncan Sands | 26641d7 | 2012-02-10 14:31:24 +0000 | [diff] [blame] | 420 | |
| 421 | // The remaining cases only make sense if the select condition has the same |
| 422 | // type as the result of the comparison, so bail out if this is not so. |
| 423 | if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy()) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 424 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 425 | // If the false value simplified to false, then the result of the compare |
| 426 | // is equal to "Cond && TCmp". This also catches the case when the false |
| 427 | // value simplified to false and the true value to true, returning "Cond". |
| 428 | if (match(FCmp, m_Zero())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 429 | if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 430 | return V; |
| 431 | // If the true value simplified to true, then the result of the compare |
| 432 | // is equal to "Cond || FCmp". |
| 433 | if (match(TCmp, m_One())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 434 | if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 435 | return V; |
| 436 | // Finally, if the false value simplified to true and the true value to |
| 437 | // false, then the result of the compare is equal to "!Cond". |
| 438 | if (match(FCmp, m_One()) && match(TCmp, m_Zero())) |
| 439 | if (Value *V = |
| 440 | SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 441 | Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 442 | return V; |
| 443 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 444 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 447 | /// In the case of a binary operation with an operand that is a PHI instruction, |
| 448 | /// try to simplify the binop by seeing whether evaluating it on the incoming |
| 449 | /// phi values yields the same result for every value. If so returns the common |
| 450 | /// value, otherwise returns null. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 451 | static Value *ThreadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS, |
| 452 | Value *RHS, const Query &Q, |
| 453 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 454 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 455 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 456 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 457 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 458 | PHINode *PI; |
| 459 | if (isa<PHINode>(LHS)) { |
| 460 | PI = cast<PHINode>(LHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 461 | // 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] | 462 | if (!ValueDominatesPHI(RHS, 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 | } else { |
| 465 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 466 | PI = cast<PHINode>(RHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 467 | // 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] | 468 | if (!ValueDominatesPHI(LHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 469 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 473 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 474 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 475 | // 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] | 476 | if (Incoming == PI) continue; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 477 | Value *V = PI == LHS ? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 478 | SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) : |
| 479 | SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 480 | // If the operation failed to simplify, or simplified to a different value |
| 481 | // to previously, then give up. |
| 482 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 483 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 484 | CommonValue = V; |
| 485 | } |
| 486 | |
| 487 | return CommonValue; |
| 488 | } |
| 489 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 490 | /// In the case of a comparison with a PHI instruction, try to simplify the |
| 491 | /// comparison by seeing whether comparing with all of the incoming phi values |
| 492 | /// yields the same result every time. If so returns the common result, |
| 493 | /// otherwise returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 494 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 495 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 496 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 497 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 498 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 499 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 500 | // Make sure the phi is on the LHS. |
| 501 | if (!isa<PHINode>(LHS)) { |
| 502 | std::swap(LHS, RHS); |
| 503 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 504 | } |
| 505 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 506 | PHINode *PI = cast<PHINode>(LHS); |
| 507 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 508 | // 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] | 509 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 510 | return nullptr; |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 511 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 512 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 513 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 514 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 515 | // 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] | 516 | if (Incoming == PI) continue; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 517 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 518 | // If the operation failed to simplify, or simplified to a different value |
| 519 | // to previously, then give up. |
| 520 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 521 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 522 | CommonValue = V; |
| 523 | } |
| 524 | |
| 525 | return CommonValue; |
| 526 | } |
| 527 | |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 528 | static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode, |
| 529 | Value *&Op0, Value *&Op1, |
| 530 | const Query &Q) { |
| 531 | if (auto *CLHS = dyn_cast<Constant>(Op0)) { |
| 532 | if (auto *CRHS = dyn_cast<Constant>(Op1)) |
| 533 | return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL); |
| 534 | |
| 535 | // Canonicalize the constant to the RHS if this is a commutative operation. |
| 536 | if (Instruction::isCommutative(Opcode)) |
| 537 | std::swap(Op0, Op1); |
| 538 | } |
| 539 | return nullptr; |
| 540 | } |
| 541 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 542 | /// Given operands for an Add, see if we can fold the result. |
| 543 | /// If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 544 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 545 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 546 | if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q)) |
| 547 | return C; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 548 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 549 | // X + undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 550 | if (match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 551 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 552 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 553 | // X + 0 -> X |
| 554 | if (match(Op1, m_Zero())) |
| 555 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 556 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 557 | // X + (Y - X) -> Y |
| 558 | // (Y - X) + X -> Y |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 559 | // Eg: X + -X -> 0 |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 560 | Value *Y = nullptr; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 561 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 562 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 563 | return Y; |
| 564 | |
| 565 | // X + ~X -> -1 since ~X = -X-1 |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 566 | Type *Ty = Op0->getType(); |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 567 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 568 | match(Op1, m_Not(m_Specific(Op0)))) |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 569 | return Constant::getAllOnesValue(Ty); |
| 570 | |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 571 | // add nsw/nuw (xor Y, signmask), signmask --> Y |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 572 | // The no-wrapping add guarantees that the top bit will be set by the add. |
| 573 | // Therefore, the xor must be clearing the already set sign bit of Y. |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 574 | if ((isNSW || isNUW) && match(Op1, m_SignMask()) && |
| 575 | match(Op0, m_Xor(m_Value(Y), m_SignMask()))) |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 576 | return Y; |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 577 | |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 578 | /// i1 add -> xor. |
Craig Topper | aa5f524 | 2017-04-06 05:28:41 +0000 | [diff] [blame] | 579 | if (MaxRecurse && Op0->getType()->getScalarType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 580 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 581 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 582 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 583 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 584 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 585 | MaxRecurse)) |
| 586 | return V; |
| 587 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 588 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 589 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 590 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 591 | // only if B and C are equal. If B and C are equal then (since we assume |
| 592 | // that operands have already been simplified) "select(cond, B, C)" should |
| 593 | // have been simplified to the common value of B and C already. Analysing |
| 594 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 595 | // for threading over phi nodes. |
| 596 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 597 | return nullptr; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 600 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 601 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 602 | const DominatorTree *DT, AssumptionCache *AC, |
| 603 | const Instruction *CxtI) { |
| 604 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 605 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 608 | /// \brief Compute the base pointer and cumulative constant offsets for V. |
| 609 | /// |
| 610 | /// This strips all constant offsets off of V, leaving it the base pointer, and |
| 611 | /// accumulates the total constant offset applied in the returned constant. It |
| 612 | /// returns 0 if V is not a pointer, and returns the constant '0' if there are |
| 613 | /// no constant offsets applied. |
Dan Gohman | 36fa839 | 2013-01-31 02:45:26 +0000 | [diff] [blame] | 614 | /// |
| 615 | /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't |
| 616 | /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc. |
| 617 | /// folding. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 618 | static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V, |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 619 | bool AllowNonInbounds = false) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 620 | assert(V->getType()->getScalarType()->isPointerTy()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 621 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 622 | Type *IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType(); |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 623 | APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 624 | |
| 625 | // Even though we don't look through PHI nodes, we could be called on an |
| 626 | // instruction in an unreachable block, which may be on a cycle. |
| 627 | SmallPtrSet<Value *, 4> Visited; |
| 628 | Visited.insert(V); |
| 629 | do { |
| 630 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 631 | if ((!AllowNonInbounds && !GEP->isInBounds()) || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 632 | !GEP->accumulateConstantOffset(DL, Offset)) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 633 | break; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 634 | V = GEP->getPointerOperand(); |
| 635 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 636 | V = cast<Operator>(V)->getOperand(0); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 637 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 638 | if (GA->isInterposable()) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 639 | break; |
| 640 | V = GA->getAliasee(); |
| 641 | } else { |
Hal Finkel | 2cac58f | 2016-07-11 03:37:59 +0000 | [diff] [blame] | 642 | if (auto CS = CallSite(V)) |
| 643 | if (Value *RV = CS.getReturnedArgOperand()) { |
| 644 | V = RV; |
| 645 | continue; |
| 646 | } |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 647 | break; |
| 648 | } |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 649 | assert(V->getType()->getScalarType()->isPointerTy() && |
| 650 | "Unexpected operand type!"); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 651 | } while (Visited.insert(V).second); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 652 | |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 653 | Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset); |
| 654 | if (V->getType()->isVectorTy()) |
| 655 | return ConstantVector::getSplat(V->getType()->getVectorNumElements(), |
| 656 | OffsetIntPtr); |
| 657 | return OffsetIntPtr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /// \brief Compute the constant difference between two pointer values. |
| 661 | /// If the difference is not a constant, returns zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 662 | static Constant *computePointerDifference(const DataLayout &DL, Value *LHS, |
| 663 | Value *RHS) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 664 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 665 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 666 | |
| 667 | // If LHS and RHS are not related via constant offsets to the same base |
| 668 | // value, there is nothing we can do here. |
| 669 | if (LHS != RHS) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 670 | return nullptr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 671 | |
| 672 | // Otherwise, the difference of LHS - RHS can be computed as: |
| 673 | // LHS - RHS |
| 674 | // = (LHSOffset + Base) - (RHSOffset + Base) |
| 675 | // = LHSOffset - RHSOffset |
| 676 | return ConstantExpr::getSub(LHSOffset, RHSOffset); |
| 677 | } |
| 678 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 679 | /// Given operands for a Sub, see if we can fold the result. |
| 680 | /// If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 681 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 682 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 683 | if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q)) |
| 684 | return C; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 685 | |
| 686 | // X - undef -> undef |
| 687 | // undef - X -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 688 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 689 | return UndefValue::get(Op0->getType()); |
| 690 | |
| 691 | // X - 0 -> X |
| 692 | if (match(Op1, m_Zero())) |
| 693 | return Op0; |
| 694 | |
| 695 | // X - X -> 0 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 696 | if (Op0 == Op1) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 697 | return Constant::getNullValue(Op0->getType()); |
| 698 | |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 699 | // Is this a negation? |
| 700 | if (match(Op0, m_Zero())) { |
| 701 | // 0 - X -> 0 if the sub is NUW. |
| 702 | if (isNUW) |
| 703 | return Op0; |
| 704 | |
| 705 | unsigned BitWidth = Op1->getType()->getScalarSizeInBits(); |
| 706 | APInt KnownZero(BitWidth, 0); |
| 707 | APInt KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 708 | computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
Craig Topper | 6856d34 | 2017-03-30 22:10:54 +0000 | [diff] [blame] | 709 | if (KnownZero.isMaxSignedValue()) { |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 710 | // Op1 is either 0 or the minimum signed value. If the sub is NSW, then |
| 711 | // Op1 must be 0 because negating the minimum signed value is undefined. |
| 712 | if (isNSW) |
| 713 | return Op0; |
| 714 | |
| 715 | // 0 - X -> X if X is 0 or the minimum signed value. |
| 716 | return Op1; |
| 717 | } |
| 718 | } |
David Majnemer | cd4fbcd | 2014-07-31 04:49:18 +0000 | [diff] [blame] | 719 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 720 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 721 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
Dinesh Dwivedi | 99281a0 | 2014-06-26 08:57:33 +0000 | [diff] [blame] | 722 | Value *X = nullptr, *Y = nullptr, *Z = Op1; |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 723 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 724 | // See if "V === Y - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 725 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 726 | // It does! Now see if "X + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 727 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, 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 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 733 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 734 | // It does! Now see if "Y + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 735 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 736 | // It does, we successfully reassociated! |
| 737 | ++NumReassoc; |
| 738 | return W; |
| 739 | } |
| 740 | } |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 741 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 742 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 743 | // For example, X - (X + 1) -> -1 |
| 744 | X = Op0; |
| 745 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 746 | // See if "V === X - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 747 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 748 | // It does! Now see if "V - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 749 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 750 | // It does, we successfully reassociated! |
| 751 | ++NumReassoc; |
| 752 | return W; |
| 753 | } |
| 754 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 755 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 756 | // It does! Now see if "V - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 757 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 758 | // It does, we successfully reassociated! |
| 759 | ++NumReassoc; |
| 760 | return W; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 765 | // For example, X - (X - Y) -> Y. |
| 766 | Z = Op0; |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 767 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 768 | // See if "V === Z - X" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 769 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 770 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 771 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 772 | // It does, we successfully reassociated! |
| 773 | ++NumReassoc; |
| 774 | return W; |
| 775 | } |
| 776 | |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 777 | // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. |
| 778 | if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) && |
| 779 | match(Op1, m_Trunc(m_Value(Y)))) |
| 780 | if (X->getType() == Y->getType()) |
| 781 | // See if "V === X - Y" simplifies. |
| 782 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
| 783 | // It does! Now see if "trunc V" simplifies. |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 784 | if (Value *W = SimplifyCastInst(Instruction::Trunc, V, Op0->getType(), |
| 785 | Q, MaxRecurse - 1)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 786 | // It does, return the simplified "trunc V". |
| 787 | return W; |
| 788 | |
| 789 | // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 790 | if (match(Op0, m_PtrToInt(m_Value(X))) && |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 791 | match(Op1, m_PtrToInt(m_Value(Y)))) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 792 | if (Constant *Result = computePointerDifference(Q.DL, X, Y)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 793 | return ConstantExpr::getIntegerCast(Result, Op0->getType(), true); |
| 794 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 795 | // i1 sub -> xor. |
Craig Topper | aa5f524 | 2017-04-06 05:28:41 +0000 | [diff] [blame] | 796 | if (MaxRecurse && Op0->getType()->getScalarType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 797 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 798 | return V; |
| 799 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 800 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 801 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 802 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 803 | // only if B and C are equal. If B and C are equal then (since we assume |
| 804 | // that operands have already been simplified) "select(cond, B, C)" should |
| 805 | // have been simplified to the common value of B and C already. Analysing |
| 806 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 807 | // for threading over phi nodes. |
| 808 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 809 | return nullptr; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 812 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 813 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 814 | const DominatorTree *DT, AssumptionCache *AC, |
| 815 | const Instruction *CxtI) { |
| 816 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 817 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 820 | /// Given operands for an FAdd, see if we can fold the result. If not, this |
| 821 | /// returns null. |
| 822 | static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 823 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 824 | if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q)) |
| 825 | return C; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 826 | |
| 827 | // fadd X, -0 ==> X |
| 828 | if (match(Op1, m_NegZero())) |
| 829 | return Op0; |
| 830 | |
| 831 | // fadd X, 0 ==> X, when we know X is not -0 |
| 832 | if (match(Op1, m_Zero()) && |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 833 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 834 | return Op0; |
| 835 | |
| 836 | // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0 |
| 837 | // where nnan and ninf have to occur at least once somewhere in this |
| 838 | // expression |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 839 | Value *SubOp = nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 840 | if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0)))) |
| 841 | SubOp = Op1; |
| 842 | else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1)))) |
| 843 | SubOp = Op0; |
| 844 | if (SubOp) { |
| 845 | Instruction *FSub = cast<Instruction>(SubOp); |
| 846 | if ((FMF.noNaNs() || FSub->hasNoNaNs()) && |
| 847 | (FMF.noInfs() || FSub->hasNoInfs())) |
| 848 | return Constant::getNullValue(Op0->getType()); |
| 849 | } |
| 850 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 851 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /// Given operands for an FSub, see if we can fold the result. If not, this |
| 855 | /// returns null. |
| 856 | static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 857 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 858 | if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q)) |
| 859 | return C; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 860 | |
| 861 | // fsub X, 0 ==> X |
| 862 | if (match(Op1, m_Zero())) |
| 863 | return Op0; |
| 864 | |
| 865 | // fsub X, -0 ==> X, when we know X is not -0 |
| 866 | if (match(Op1, m_NegZero()) && |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 867 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 868 | return Op0; |
| 869 | |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 870 | // fsub -0.0, (fsub -0.0, X) ==> X |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 871 | Value *X; |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 872 | if (match(Op0, m_NegZero()) && match(Op1, m_FSub(m_NegZero(), m_Value(X)))) |
| 873 | return X; |
| 874 | |
| 875 | // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored. |
Benjamin Kramer | 6bb1502 | 2016-02-29 12:18:25 +0000 | [diff] [blame] | 876 | if (FMF.noSignedZeros() && match(Op0, m_AnyZero()) && |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 877 | match(Op1, m_FSub(m_AnyZero(), m_Value(X)))) |
| 878 | return X; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 879 | |
Benjamin Kramer | 228680d | 2015-06-14 21:01:20 +0000 | [diff] [blame] | 880 | // fsub nnan x, x ==> 0.0 |
| 881 | if (FMF.noNaNs() && Op0 == Op1) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 882 | return Constant::getNullValue(Op0->getType()); |
| 883 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 884 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 887 | /// Given the operands for an FMul, see if we can fold the result |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 888 | static Value *SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 889 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 890 | if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q)) |
| 891 | return C; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 892 | |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 893 | // fmul X, 1.0 ==> X |
| 894 | if (match(Op1, m_FPOne())) |
| 895 | return Op0; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 896 | |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 897 | // fmul nnan nsz X, 0 ==> 0 |
| 898 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero())) |
| 899 | return Op1; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 900 | |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 901 | return nullptr; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 904 | /// Given operands for a Mul, see if we can fold the result. |
| 905 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 906 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q, |
| 907 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 908 | if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q)) |
| 909 | return C; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 910 | |
| 911 | // X * undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 912 | if (match(Op1, m_Undef())) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 913 | return Constant::getNullValue(Op0->getType()); |
| 914 | |
| 915 | // X * 0 -> 0 |
| 916 | if (match(Op1, m_Zero())) |
| 917 | return Op1; |
| 918 | |
| 919 | // X * 1 -> X |
| 920 | if (match(Op1, m_One())) |
| 921 | return Op0; |
| 922 | |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 923 | // (X / Y) * Y -> X if the division is exact. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 924 | Value *X = nullptr; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 925 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 926 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 927 | return X; |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 928 | |
Nick Lewycky | b89d9a4 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 929 | // i1 mul -> and. |
Craig Topper | 2f1e1c3 | 2017-04-06 17:33:37 +0000 | [diff] [blame] | 930 | if (MaxRecurse && Op0->getType()->getScalarType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 931 | if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 932 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 933 | |
| 934 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 935 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 936 | MaxRecurse)) |
| 937 | return V; |
| 938 | |
| 939 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 940 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 941 | Q, MaxRecurse)) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 942 | return V; |
| 943 | |
| 944 | // If the operation is with the result of a select instruction, check whether |
| 945 | // operating on either branch of the select always yields the same value. |
| 946 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 947 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 948 | MaxRecurse)) |
| 949 | return V; |
| 950 | |
| 951 | // If the operation is with the result of a phi instruction, check whether |
| 952 | // operating on all incoming values of the phi always yields the same value. |
| 953 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 954 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 955 | MaxRecurse)) |
| 956 | return V; |
| 957 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 958 | return nullptr; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 961 | Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 962 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 963 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 964 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 965 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 966 | return ::SimplifyFAddInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 967 | RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 971 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 972 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 973 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 974 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 975 | return ::SimplifyFSubInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 976 | RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 979 | Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 980 | const DataLayout &DL, |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 981 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 982 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 983 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 984 | return ::SimplifyFMulInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 985 | RecursionLimit); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 988 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 989 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 990 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 991 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 992 | return ::SimplifyMulInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 993 | RecursionLimit); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 996 | /// Check for common or similar folds of integer division or integer remainder. |
| 997 | static Value *simplifyDivRem(Value *Op0, Value *Op1, bool IsDiv) { |
| 998 | Type *Ty = Op0->getType(); |
| 999 | |
| 1000 | // X / undef -> undef |
| 1001 | // X % undef -> undef |
| 1002 | if (match(Op1, m_Undef())) |
| 1003 | return Op1; |
| 1004 | |
| 1005 | // X / 0 -> undef |
| 1006 | // X % 0 -> undef |
| 1007 | // We don't need to preserve faults! |
| 1008 | if (match(Op1, m_Zero())) |
| 1009 | return UndefValue::get(Ty); |
| 1010 | |
Sanjay Patel | 2b1f6f4 | 2017-03-09 16:20:52 +0000 | [diff] [blame] | 1011 | // If any element of a constant divisor vector is zero, the whole op is undef. |
| 1012 | auto *Op1C = dyn_cast<Constant>(Op1); |
| 1013 | if (Op1C && Ty->isVectorTy()) { |
| 1014 | unsigned NumElts = Ty->getVectorNumElements(); |
| 1015 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1016 | Constant *Elt = Op1C->getAggregateElement(i); |
| 1017 | if (Elt && Elt->isNullValue()) |
| 1018 | return UndefValue::get(Ty); |
| 1019 | } |
| 1020 | } |
| 1021 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1022 | // undef / X -> 0 |
| 1023 | // undef % X -> 0 |
| 1024 | if (match(Op0, m_Undef())) |
| 1025 | return Constant::getNullValue(Ty); |
| 1026 | |
| 1027 | // 0 / X -> 0 |
| 1028 | // 0 % X -> 0 |
| 1029 | if (match(Op0, m_Zero())) |
| 1030 | return Op0; |
| 1031 | |
| 1032 | // X / X -> 1 |
| 1033 | // X % X -> 0 |
| 1034 | if (Op0 == Op1) |
| 1035 | return IsDiv ? ConstantInt::get(Ty, 1) : Constant::getNullValue(Ty); |
| 1036 | |
| 1037 | // X / 1 -> X |
| 1038 | // X % 1 -> 0 |
Sanjay Patel | 962a843 | 2017-03-09 21:56:03 +0000 | [diff] [blame] | 1039 | // If this is a boolean op (single-bit element type), we can't have |
| 1040 | // division-by-zero or remainder-by-zero, so assume the divisor is 1. |
| 1041 | if (match(Op1, m_One()) || Ty->getScalarType()->isIntegerTy(1)) |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1042 | return IsDiv ? Op0 : Constant::getNullValue(Ty); |
| 1043 | |
| 1044 | return nullptr; |
| 1045 | } |
| 1046 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1047 | /// Given operands for an SDiv or UDiv, see if we can fold the result. |
| 1048 | /// If not, this returns null. |
Anders Carlsson | 36c6d23 | 2011-02-05 18:33:43 +0000 | [diff] [blame] | 1049 | static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1050 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1051 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
| 1052 | return C; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1053 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1054 | if (Value *V = simplifyDivRem(Op0, Op1, true)) |
| 1055 | return V; |
| 1056 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1057 | bool isSigned = Opcode == Instruction::SDiv; |
| 1058 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1059 | // (X * Y) / Y -> X if the multiplication does not overflow. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1060 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1061 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 1062 | 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] | 1063 | OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); |
Duncan Sands | 5747aba | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 1064 | // If the Mul knows it does not overflow, then we are good to go. |
| 1065 | if ((isSigned && Mul->hasNoSignedWrap()) || |
| 1066 | (!isSigned && Mul->hasNoUnsignedWrap())) |
| 1067 | return X; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1068 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 1069 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 1070 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 1071 | return X; |
| 1072 | } |
| 1073 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1074 | // (X rem Y) / Y -> 0 |
| 1075 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1076 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 1077 | return Constant::getNullValue(Op0->getType()); |
| 1078 | |
David Majnemer | cb9d596 | 2014-10-11 10:20:01 +0000 | [diff] [blame] | 1079 | // (X /u C1) /u C2 -> 0 if C1 * C2 overflow |
| 1080 | ConstantInt *C1, *C2; |
| 1081 | if (!isSigned && match(Op0, m_UDiv(m_Value(X), m_ConstantInt(C1))) && |
| 1082 | match(Op1, m_ConstantInt(C2))) { |
| 1083 | bool Overflow; |
Craig Topper | 9b71a40 | 2017-04-19 21:09:45 +0000 | [diff] [blame] | 1084 | (void)C1->getValue().umul_ov(C2->getValue(), Overflow); |
David Majnemer | cb9d596 | 2014-10-11 10:20:01 +0000 | [diff] [blame] | 1085 | if (Overflow) |
| 1086 | return Constant::getNullValue(Op0->getType()); |
| 1087 | } |
| 1088 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1089 | // If the operation is with the result of a select instruction, check whether |
| 1090 | // operating on either branch of the select always yields the same value. |
| 1091 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1092 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1093 | return V; |
| 1094 | |
| 1095 | // If the operation is with the result of a phi instruction, check whether |
| 1096 | // operating on all incoming values of the phi always yields the same value. |
| 1097 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1098 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1099 | return V; |
| 1100 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1101 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1104 | /// Given operands for an SDiv, see if we can fold the result. |
| 1105 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1106 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1107 | unsigned MaxRecurse) { |
| 1108 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1109 | return V; |
| 1110 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1111 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1114 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1115 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1116 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1117 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1118 | return ::SimplifySDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1119 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1122 | /// Given operands for a UDiv, see if we can fold the result. |
| 1123 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1124 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1125 | unsigned MaxRecurse) { |
| 1126 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1127 | return V; |
| 1128 | |
David Majnemer | 63da0c2 | 2017-01-06 22:58:02 +0000 | [diff] [blame] | 1129 | // udiv %V, C -> 0 if %V < C |
| 1130 | if (MaxRecurse) { |
| 1131 | if (Constant *C = dyn_cast_or_null<Constant>(SimplifyICmpInst( |
| 1132 | ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse - 1))) { |
| 1133 | if (C->isAllOnesValue()) { |
| 1134 | return Constant::getNullValue(Op0->getType()); |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1139 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1142 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1143 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1144 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1145 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1146 | return ::SimplifyUDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1147 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1150 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 1151 | const Query &Q, unsigned) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1152 | if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q)) |
| 1153 | return C; |
| 1154 | |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1155 | // undef / X -> undef (the undef could be a snan). |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1156 | if (match(Op0, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1157 | return Op0; |
| 1158 | |
| 1159 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1160 | if (match(Op1, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1161 | return Op1; |
| 1162 | |
Zia Ansari | 394cef8 | 2016-12-08 23:27:40 +0000 | [diff] [blame] | 1163 | // X / 1.0 -> X |
| 1164 | if (match(Op1, m_FPOne())) |
| 1165 | return Op0; |
| 1166 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1167 | // 0 / X -> 0 |
| 1168 | // Requires that NaNs are off (X could be zero) and signed zeroes are |
| 1169 | // ignored (X could be positive or negative, so the output sign is unknown). |
| 1170 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero())) |
| 1171 | return Op0; |
| 1172 | |
Benjamin Kramer | 1ee59cb | 2015-06-16 14:57:29 +0000 | [diff] [blame] | 1173 | if (FMF.noNaNs()) { |
| 1174 | // X / X -> 1.0 is legal when NaNs are ignored. |
Benjamin Kramer | 4f05246 | 2015-06-14 18:53:58 +0000 | [diff] [blame] | 1175 | if (Op0 == Op1) |
| 1176 | return ConstantFP::get(Op0->getType(), 1.0); |
| 1177 | |
| 1178 | // -X / X -> -1.0 and |
Benjamin Kramer | 1ee59cb | 2015-06-16 14:57:29 +0000 | [diff] [blame] | 1179 | // X / -X -> -1.0 are legal when NaNs are ignored. |
Benjamin Kramer | 4f05246 | 2015-06-14 18:53:58 +0000 | [diff] [blame] | 1180 | // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored. |
| 1181 | if ((BinaryOperator::isFNeg(Op0, /*IgnoreZeroSign=*/true) && |
| 1182 | BinaryOperator::getFNegArgument(Op0) == Op1) || |
| 1183 | (BinaryOperator::isFNeg(Op1, /*IgnoreZeroSign=*/true) && |
| 1184 | BinaryOperator::getFNegArgument(Op1) == Op0)) |
| 1185 | return ConstantFP::get(Op0->getType(), -1.0); |
| 1186 | } |
| 1187 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1188 | return nullptr; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1191 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1192 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1193 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1194 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1195 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1196 | return ::SimplifyFDivInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1197 | RecursionLimit); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1200 | /// Given operands for an SRem or URem, see if we can fold the result. |
| 1201 | /// If not, this returns null. |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1202 | static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1203 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1204 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
| 1205 | return C; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1206 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1207 | if (Value *V = simplifyDivRem(Op0, Op1, false)) |
| 1208 | return V; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1209 | |
David Majnemer | b435a42 | 2014-09-17 04:16:35 +0000 | [diff] [blame] | 1210 | // (X % Y) % Y -> X % Y |
| 1211 | if ((Opcode == Instruction::SRem && |
| 1212 | match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1213 | (Opcode == Instruction::URem && |
| 1214 | match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1215 | return Op0; |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1216 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1217 | // If the operation is with the result of a select instruction, check whether |
| 1218 | // operating on either branch of the select always yields the same value. |
| 1219 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1220 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1221 | return V; |
| 1222 | |
| 1223 | // If the operation is with the result of a phi instruction, check whether |
| 1224 | // operating on all incoming values of the phi always yields the same value. |
| 1225 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1226 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1227 | return V; |
| 1228 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1229 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1232 | /// Given operands for an SRem, see if we can fold the result. |
| 1233 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1234 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q, |
| 1235 | unsigned MaxRecurse) { |
| 1236 | if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1237 | return V; |
| 1238 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1239 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1242 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1243 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1244 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1245 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1246 | return ::SimplifySRemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1247 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1250 | /// Given operands for a URem, see if we can fold the result. |
| 1251 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1252 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1253 | unsigned MaxRecurse) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1254 | if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1255 | return V; |
| 1256 | |
David Majnemer | 8c0e62f | 2017-01-06 21:23:51 +0000 | [diff] [blame] | 1257 | // urem %V, C -> %V if %V < C |
| 1258 | if (MaxRecurse) { |
| 1259 | if (Constant *C = dyn_cast_or_null<Constant>(SimplifyICmpInst( |
| 1260 | ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse - 1))) { |
| 1261 | if (C->isAllOnesValue()) { |
| 1262 | return Op0; |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1267 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1270 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1271 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1272 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1273 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1274 | return ::SimplifyURemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1275 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1278 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1279 | const Query &Q, unsigned) { |
| 1280 | if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q)) |
| 1281 | return C; |
| 1282 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1283 | // undef % X -> undef (the undef could be a snan). |
| 1284 | if (match(Op0, m_Undef())) |
| 1285 | return Op0; |
| 1286 | |
| 1287 | // X % undef -> undef |
| 1288 | if (match(Op1, m_Undef())) |
| 1289 | return Op1; |
| 1290 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1291 | // 0 % X -> 0 |
| 1292 | // Requires that NaNs are off (X could be zero) and signed zeroes are |
| 1293 | // ignored (X could be positive or negative, so the output sign is unknown). |
| 1294 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero())) |
| 1295 | return Op0; |
| 1296 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1297 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1300 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1301 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1302 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1303 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1304 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1305 | return ::SimplifyFRemInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1306 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1309 | /// Returns true if a shift by \c Amount always yields undef. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1310 | static bool isUndefShift(Value *Amount) { |
| 1311 | Constant *C = dyn_cast<Constant>(Amount); |
| 1312 | if (!C) |
| 1313 | return false; |
| 1314 | |
| 1315 | // X shift by undef -> undef because it may shift by the bitwidth. |
| 1316 | if (isa<UndefValue>(C)) |
| 1317 | return true; |
| 1318 | |
| 1319 | // Shifting by the bitwidth or more is undefined. |
| 1320 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1321 | if (CI->getValue().getLimitedValue() >= |
| 1322 | CI->getType()->getScalarSizeInBits()) |
| 1323 | return true; |
| 1324 | |
| 1325 | // If all lanes of a vector shift are undefined the whole shift is. |
| 1326 | if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) { |
| 1327 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; ++I) |
| 1328 | if (!isUndefShift(C->getAggregateElement(I))) |
| 1329 | return false; |
| 1330 | return true; |
| 1331 | } |
| 1332 | |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1336 | /// Given operands for an Shl, LShr or AShr, see if we can fold the result. |
| 1337 | /// If not, this returns null. |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1338 | static Value *SimplifyShift(Instruction::BinaryOps Opcode, Value *Op0, |
| 1339 | Value *Op1, const Query &Q, unsigned MaxRecurse) { |
| 1340 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
| 1341 | return C; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1342 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1343 | // 0 shift by X -> 0 |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1344 | if (match(Op0, m_Zero())) |
| 1345 | return Op0; |
| 1346 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1347 | // X shift by 0 -> X |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1348 | if (match(Op1, m_Zero())) |
| 1349 | return Op0; |
| 1350 | |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1351 | // Fold undefined shifts. |
| 1352 | if (isUndefShift(Op1)) |
| 1353 | return UndefValue::get(Op0->getType()); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1354 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1355 | // If the operation is with the result of a select instruction, check whether |
| 1356 | // operating on either branch of the select always yields the same value. |
| 1357 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1358 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1359 | return V; |
| 1360 | |
| 1361 | // If the operation is with the result of a phi instruction, check whether |
| 1362 | // operating on all incoming values of the phi always yields the same value. |
| 1363 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1364 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1365 | return V; |
| 1366 | |
Sanjay Patel | 6786bc5 | 2016-05-10 20:46:54 +0000 | [diff] [blame] | 1367 | // If any bits in the shift amount make that value greater than or equal to |
| 1368 | // the number of bits in the type, the shift is undefined. |
| 1369 | unsigned BitWidth = Op1->getType()->getScalarSizeInBits(); |
| 1370 | APInt KnownZero(BitWidth, 0); |
| 1371 | APInt KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1372 | computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
Sanjay Patel | 6786bc5 | 2016-05-10 20:46:54 +0000 | [diff] [blame] | 1373 | if (KnownOne.getLimitedValue() >= BitWidth) |
| 1374 | return UndefValue::get(Op0->getType()); |
| 1375 | |
| 1376 | // If all valid bits in the shift amount are known zero, the first operand is |
| 1377 | // unchanged. |
| 1378 | unsigned NumValidShiftBits = Log2_32_Ceil(BitWidth); |
| 1379 | APInt ShiftAmountMask = APInt::getLowBitsSet(BitWidth, NumValidShiftBits); |
| 1380 | if ((KnownZero & ShiftAmountMask) == ShiftAmountMask) |
| 1381 | return Op0; |
| 1382 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1383 | return nullptr; |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1386 | /// \brief Given operands for an Shl, LShr or AShr, see if we can |
| 1387 | /// fold the result. If not, this returns null. |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1388 | static Value *SimplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0, |
| 1389 | Value *Op1, bool isExact, const Query &Q, |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1390 | unsigned MaxRecurse) { |
| 1391 | if (Value *V = SimplifyShift(Opcode, Op0, Op1, Q, MaxRecurse)) |
| 1392 | return V; |
| 1393 | |
| 1394 | // X >> X -> 0 |
| 1395 | if (Op0 == Op1) |
| 1396 | return Constant::getNullValue(Op0->getType()); |
| 1397 | |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1398 | // undef >> X -> 0 |
| 1399 | // undef >> X -> undef (if it's exact) |
| 1400 | if (match(Op0, m_Undef())) |
| 1401 | return isExact ? Op0 : Constant::getNullValue(Op0->getType()); |
| 1402 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1403 | // The low bit cannot be shifted out of an exact shift if it is set. |
| 1404 | if (isExact) { |
| 1405 | unsigned BitWidth = Op0->getType()->getScalarSizeInBits(); |
| 1406 | APInt Op0KnownZero(BitWidth, 0); |
| 1407 | APInt Op0KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1408 | computeKnownBits(Op0, Op0KnownZero, Op0KnownOne, Q.DL, /*Depth=*/0, Q.AC, |
| 1409 | Q.CxtI, Q.DT); |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1410 | if (Op0KnownOne[0]) |
| 1411 | return Op0; |
| 1412 | } |
| 1413 | |
| 1414 | return nullptr; |
| 1415 | } |
| 1416 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1417 | /// Given operands for an Shl, see if we can fold the result. |
| 1418 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1419 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1420 | const Query &Q, unsigned MaxRecurse) { |
| 1421 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1422 | return V; |
| 1423 | |
| 1424 | // undef << X -> 0 |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1425 | // undef << X -> undef if (if it's NSW/NUW) |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1426 | if (match(Op0, m_Undef())) |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1427 | return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType()); |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1429 | // (X >> A) << A -> X |
| 1430 | Value *X; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1431 | 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] | 1432 | return X; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1433 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1436 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1437 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1438 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1439 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1440 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, 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 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1444 | /// Given operands for an LShr, see if we can fold the result. |
| 1445 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1446 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1447 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1448 | if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q, |
| 1449 | MaxRecurse)) |
| 1450 | return V; |
David Majnemer | a80fed7 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1452 | // (X << A) >> A -> X |
| 1453 | Value *X; |
David Majnemer | 4f43837 | 2014-11-04 17:38:50 +0000 | [diff] [blame] | 1454 | if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1455 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1456 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1457 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1460 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1461 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1462 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1463 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1464 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1465 | return ::SimplifyLShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1466 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1469 | /// Given operands for an AShr, see if we can fold the result. |
| 1470 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1471 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1472 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1473 | if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q, |
| 1474 | MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1475 | return V; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1476 | |
| 1477 | // all ones >>a X -> all ones |
| 1478 | if (match(Op0, m_AllOnes())) |
| 1479 | return Op0; |
| 1480 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1481 | // (X << A) >> A -> X |
| 1482 | Value *X; |
David Majnemer | 2de97fc | 2014-11-04 17:47:13 +0000 | [diff] [blame] | 1483 | if (match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1484 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1485 | |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1486 | // Arithmetic shifting an all-sign-bit value is a no-op. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1487 | unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1488 | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
| 1489 | return Op0; |
| 1490 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1491 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1494 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1495 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1496 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1497 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1498 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1499 | return ::SimplifyAShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1500 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1503 | static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, |
| 1504 | ICmpInst *UnsignedICmp, bool IsAnd) { |
| 1505 | Value *X, *Y; |
| 1506 | |
| 1507 | ICmpInst::Predicate EqPred; |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1508 | if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) || |
| 1509 | !ICmpInst::isEquality(EqPred)) |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1510 | return nullptr; |
| 1511 | |
| 1512 | ICmpInst::Predicate UnsignedPred; |
| 1513 | if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) && |
| 1514 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1515 | ; |
| 1516 | else if (match(UnsignedICmp, |
| 1517 | m_ICmp(UnsignedPred, m_Value(Y), m_Specific(X))) && |
| 1518 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1519 | UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); |
| 1520 | else |
| 1521 | return nullptr; |
| 1522 | |
| 1523 | // X < Y && Y != 0 --> X < Y |
| 1524 | // X < Y || Y != 0 --> Y != 0 |
| 1525 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE) |
| 1526 | return IsAnd ? UnsignedICmp : ZeroICmp; |
| 1527 | |
| 1528 | // X >= Y || Y != 0 --> true |
| 1529 | // X >= Y || Y == 0 --> X >= Y |
| 1530 | if (UnsignedPred == ICmpInst::ICMP_UGE && !IsAnd) { |
| 1531 | if (EqPred == ICmpInst::ICMP_NE) |
| 1532 | return getTrue(UnsignedICmp->getType()); |
| 1533 | return UnsignedICmp; |
| 1534 | } |
| 1535 | |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1536 | // X < Y && Y == 0 --> false |
| 1537 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ && |
| 1538 | IsAnd) |
| 1539 | return getFalse(UnsignedICmp->getType()); |
| 1540 | |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1541 | return nullptr; |
| 1542 | } |
| 1543 | |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1544 | /// Commuted variants are assumed to be handled by calling this function again |
| 1545 | /// with the parameters swapped. |
| 1546 | static Value *simplifyAndOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { |
| 1547 | ICmpInst::Predicate Pred0, Pred1; |
| 1548 | Value *A ,*B; |
Sanjay Patel | 5369775 | 2016-12-06 22:09:52 +0000 | [diff] [blame] | 1549 | if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || |
| 1550 | !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1551 | return nullptr; |
| 1552 | |
| 1553 | // We have (icmp Pred0, A, B) & (icmp Pred1, A, B). |
| 1554 | // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we |
| 1555 | // can eliminate Op1 from this 'and'. |
| 1556 | if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1)) |
| 1557 | return Op0; |
| 1558 | |
| 1559 | // Check for any combination of predicates that are guaranteed to be disjoint. |
| 1560 | if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) || |
| 1561 | (Pred0 == ICmpInst::ICMP_EQ && ICmpInst::isFalseWhenEqual(Pred1)) || |
| 1562 | (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT) || |
| 1563 | (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)) |
| 1564 | return getFalse(Op0->getType()); |
| 1565 | |
| 1566 | return nullptr; |
| 1567 | } |
| 1568 | |
| 1569 | /// Commuted variants are assumed to be handled by calling this function again |
| 1570 | /// with the parameters swapped. |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1571 | static Value *SimplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1572 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true)) |
| 1573 | return X; |
| 1574 | |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1575 | if (Value *X = simplifyAndOfICmpsWithSameOperands(Op0, Op1)) |
| 1576 | return X; |
| 1577 | |
Sanjay Patel | 35c362e | 2017-04-24 21:52:39 +0000 | [diff] [blame] | 1578 | // FIXME: This should be shared with or-of-icmps. |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1579 | // Look for this pattern: (icmp V, C0) & (icmp V, C1)). |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1580 | Type *ITy = Op0->getType(); |
| 1581 | ICmpInst::Predicate Pred0, Pred1; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1582 | const APInt *C0, *C1; |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1583 | Value *V; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1584 | if (match(Op0, m_ICmp(Pred0, m_Value(V), m_APInt(C0))) && |
| 1585 | match(Op1, m_ICmp(Pred1, m_Specific(V), m_APInt(C1)))) { |
| 1586 | // Make a constant range that's the intersection of the two icmp ranges. |
| 1587 | // If the intersection is empty, we know that the result is false. |
Sanjay Patel | 35c362e | 2017-04-24 21:52:39 +0000 | [diff] [blame] | 1588 | auto Range0 = ConstantRange::makeExactICmpRegion(Pred0, *C0); |
| 1589 | auto Range1 = ConstantRange::makeExactICmpRegion(Pred1, *C1); |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1590 | if (Range0.intersectWith(Range1).isEmptySet()) |
| 1591 | return getFalse(ITy); |
Sanjay Patel | 35c362e | 2017-04-24 21:52:39 +0000 | [diff] [blame] | 1592 | |
| 1593 | // If a range is a superset of the other, the smaller set is all we need. |
| 1594 | if (Range0.contains(Range1)) |
| 1595 | return Op1; |
| 1596 | if (Range1.contains(Range0)) |
| 1597 | return Op0; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1600 | // (icmp (add V, C0), C1) & (icmp V, C0) |
| 1601 | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1)))) |
Sanjay Patel | f8ee0e0 | 2016-06-19 17:20:27 +0000 | [diff] [blame] | 1602 | return nullptr; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1603 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1604 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1605 | return nullptr; |
| 1606 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1607 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1608 | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
| 1609 | return nullptr; |
| 1610 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1611 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1612 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1613 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1614 | const APInt Delta = *C1 - *C0; |
| 1615 | if (C0->isStrictlyPositive()) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1616 | if (Delta == 2) { |
| 1617 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT) |
| 1618 | return getFalse(ITy); |
| 1619 | if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1620 | return getFalse(ITy); |
| 1621 | } |
| 1622 | if (Delta == 1) { |
| 1623 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT) |
| 1624 | return getFalse(ITy); |
| 1625 | if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1626 | return getFalse(ITy); |
| 1627 | } |
| 1628 | } |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1629 | if (C0->getBoolValue() && isNUW) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1630 | if (Delta == 2) |
| 1631 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT) |
| 1632 | return getFalse(ITy); |
| 1633 | if (Delta == 1) |
| 1634 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT) |
| 1635 | return getFalse(ITy); |
| 1636 | } |
| 1637 | |
| 1638 | return nullptr; |
| 1639 | } |
| 1640 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1641 | /// Given operands for an And, see if we can fold the result. |
| 1642 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1643 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1644 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1645 | if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q)) |
| 1646 | return C; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1648 | // X & undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1649 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1650 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1652 | // X & X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1653 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1654 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1655 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1656 | // X & 0 = 0 |
| 1657 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1658 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1659 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1660 | // X & -1 = X |
| 1661 | if (match(Op1, m_AllOnes())) |
| 1662 | return Op0; |
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 | // A & ~A = ~A & A = 0 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1665 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1666 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1667 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1668 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1669 | // (A | ?) & A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1670 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1671 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1672 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1673 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1674 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1675 | // A & (A | ?) = A |
| 1676 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1677 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1678 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1679 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1680 | // A & (-A) = A if A is a power of two or zero. |
| 1681 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1682 | match(Op1, m_Neg(m_Specific(Op0)))) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1683 | if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1684 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1685 | return Op0; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1686 | if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1687 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1688 | return Op1; |
| 1689 | } |
| 1690 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1691 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1692 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1693 | if (Value *V = SimplifyAndOfICmps(ICILHS, ICIRHS)) |
| 1694 | return V; |
| 1695 | if (Value *V = SimplifyAndOfICmps(ICIRHS, ICILHS)) |
| 1696 | return V; |
| 1697 | } |
| 1698 | } |
| 1699 | |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1700 | // The compares may be hidden behind casts. Look through those and try the |
| 1701 | // same folds as above. |
| 1702 | auto *Cast0 = dyn_cast<CastInst>(Op0); |
| 1703 | auto *Cast1 = dyn_cast<CastInst>(Op1); |
| 1704 | if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() && |
| 1705 | Cast0->getSrcTy() == Cast1->getSrcTy()) { |
| 1706 | auto *Cmp0 = dyn_cast<ICmpInst>(Cast0->getOperand(0)); |
| 1707 | auto *Cmp1 = dyn_cast<ICmpInst>(Cast1->getOperand(0)); |
| 1708 | if (Cmp0 && Cmp1) { |
| 1709 | Instruction::CastOps CastOpc = Cast0->getOpcode(); |
| 1710 | Type *ResultType = Cast0->getType(); |
| 1711 | if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp0, Cmp1))) |
| 1712 | return ConstantExpr::getCast(CastOpc, V, ResultType); |
| 1713 | if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp1, Cmp0))) |
| 1714 | return ConstantExpr::getCast(CastOpc, V, ResultType); |
| 1715 | } |
| 1716 | } |
| 1717 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1718 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1719 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, |
| 1720 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1721 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1722 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1723 | // And distributes over Or. Try some generic simplifications based on this. |
| 1724 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1725 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1726 | return V; |
| 1727 | |
| 1728 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1729 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1730 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1731 | return V; |
| 1732 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1733 | // If the operation is with the result of a select instruction, check whether |
| 1734 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1735 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1736 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, |
| 1737 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1738 | return V; |
| 1739 | |
| 1740 | // If the operation is with the result of a phi instruction, check whether |
| 1741 | // 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] | 1742 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1743 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1744 | MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1745 | return V; |
| 1746 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1747 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1750 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1751 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1752 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1753 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1754 | return ::SimplifyAndInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1755 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Sanjay Patel | d0ccdb4 | 2016-12-06 18:09:37 +0000 | [diff] [blame] | 1758 | /// Commuted variants are assumed to be handled by calling this function again |
| 1759 | /// with the parameters swapped. |
| 1760 | static Value *simplifyOrOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { |
| 1761 | ICmpInst::Predicate Pred0, Pred1; |
| 1762 | Value *A ,*B; |
Sanjay Patel | 5369775 | 2016-12-06 22:09:52 +0000 | [diff] [blame] | 1763 | if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || |
| 1764 | !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) |
Sanjay Patel | d0ccdb4 | 2016-12-06 18:09:37 +0000 | [diff] [blame] | 1765 | return nullptr; |
| 1766 | |
| 1767 | // We have (icmp Pred0, A, B) | (icmp Pred1, A, B). |
| 1768 | // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we |
| 1769 | // can eliminate Op0 from this 'or'. |
| 1770 | if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1)) |
| 1771 | return Op1; |
| 1772 | |
| 1773 | // Check for any combination of predicates that cover the entire range of |
| 1774 | // possibilities. |
| 1775 | if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) || |
| 1776 | (Pred0 == ICmpInst::ICMP_NE && ICmpInst::isTrueWhenEqual(Pred1)) || |
| 1777 | (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGE) || |
| 1778 | (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGE)) |
| 1779 | return getTrue(Op0->getType()); |
| 1780 | |
| 1781 | return nullptr; |
| 1782 | } |
| 1783 | |
| 1784 | /// Commuted variants are assumed to be handled by calling this function again |
| 1785 | /// with the parameters swapped. |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1786 | static Value *SimplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1787 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false)) |
| 1788 | return X; |
| 1789 | |
Sanjay Patel | d0ccdb4 | 2016-12-06 18:09:37 +0000 | [diff] [blame] | 1790 | if (Value *X = simplifyOrOfICmpsWithSameOperands(Op0, Op1)) |
| 1791 | return X; |
| 1792 | |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1793 | // (icmp (add V, C0), C1) | (icmp V, C0) |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1794 | ICmpInst::Predicate Pred0, Pred1; |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1795 | const APInt *C0, *C1; |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1796 | Value *V; |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1797 | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1)))) |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1798 | return nullptr; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1799 | |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1800 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
| 1801 | return nullptr; |
| 1802 | |
| 1803 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
| 1804 | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1805 | return nullptr; |
| 1806 | |
| 1807 | Type *ITy = Op0->getType(); |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1808 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1809 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1810 | |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1811 | const APInt Delta = *C1 - *C0; |
| 1812 | if (C0->isStrictlyPositive()) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1813 | if (Delta == 2) { |
| 1814 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE) |
| 1815 | return getTrue(ITy); |
| 1816 | if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1817 | return getTrue(ITy); |
| 1818 | } |
| 1819 | if (Delta == 1) { |
| 1820 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE) |
| 1821 | return getTrue(ITy); |
| 1822 | if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1823 | return getTrue(ITy); |
| 1824 | } |
| 1825 | } |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1826 | if (C0->getBoolValue() && isNUW) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1827 | if (Delta == 2) |
| 1828 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE) |
| 1829 | return getTrue(ITy); |
| 1830 | if (Delta == 1) |
| 1831 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE) |
| 1832 | return getTrue(ITy); |
| 1833 | } |
| 1834 | |
| 1835 | return nullptr; |
| 1836 | } |
| 1837 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1838 | /// Given operands for an Or, see if we can fold the result. |
| 1839 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1840 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const Query &Q, |
| 1841 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1842 | if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q)) |
| 1843 | return C; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1844 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1845 | // X | undef -> -1 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1846 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1847 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1848 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1849 | // X | X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1850 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1851 | return Op0; |
| 1852 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1853 | // X | 0 = X |
| 1854 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1855 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1856 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1857 | // X | -1 = -1 |
| 1858 | if (match(Op1, m_AllOnes())) |
| 1859 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1860 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1861 | // A | ~A = ~A | A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1862 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1863 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1864 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1865 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1866 | // (A & ?) | A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1867 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1868 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1869 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1870 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1871 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1872 | // A | (A & ?) = A |
| 1873 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1874 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1875 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1876 | |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1877 | // ~(A & ?) | A = -1 |
| 1878 | if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1879 | (A == Op1 || B == Op1)) |
| 1880 | return Constant::getAllOnesValue(Op1->getType()); |
| 1881 | |
| 1882 | // A | ~(A & ?) = -1 |
| 1883 | if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1884 | (A == Op0 || B == Op0)) |
| 1885 | return Constant::getAllOnesValue(Op0->getType()); |
| 1886 | |
Sanjay Patel | 0889225 | 2017-04-24 18:24:36 +0000 | [diff] [blame] | 1887 | // (A & ~B) | (A ^ B) -> (A ^ B) |
| 1888 | // (~B & A) | (A ^ B) -> (A ^ B) |
Craig Topper | 0b650d3 | 2017-04-25 17:01:32 +0000 | [diff] [blame^] | 1889 | // (A & ~B) | (B ^ A) -> (B ^ A) |
| 1890 | // (~B & A) | (B ^ A) -> (B ^ A) |
| 1891 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 1892 | (match(Op0, m_c_And(m_Specific(A), m_Not(m_Specific(B)))) || |
| 1893 | match(Op0, m_c_And(m_Not(m_Specific(A)), m_Specific(B))))) |
Sanjay Patel | 0889225 | 2017-04-24 18:24:36 +0000 | [diff] [blame] | 1894 | return Op1; |
| 1895 | |
| 1896 | // Commute the 'or' operands. |
| 1897 | // (A ^ B) | (A & ~B) -> (A ^ B) |
| 1898 | // (A ^ B) | (~B & A) -> (A ^ B) |
Craig Topper | 0b650d3 | 2017-04-25 17:01:32 +0000 | [diff] [blame^] | 1899 | // (B ^ A) | (A & ~B) -> (B ^ A) |
| 1900 | // (B ^ A) | (~B & A) -> (B ^ A) |
| 1901 | if (match(Op0, m_Xor(m_Value(A), m_Value(B))) && |
| 1902 | (match(Op1, m_c_And(m_Specific(A), m_Not(m_Specific(B)))) || |
| 1903 | match(Op1, m_c_And(m_Not(m_Specific(A)), m_Specific(B))))) |
Sanjay Patel | 0889225 | 2017-04-24 18:24:36 +0000 | [diff] [blame] | 1904 | return Op0; |
| 1905 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1906 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1907 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1908 | if (Value *V = SimplifyOrOfICmps(ICILHS, ICIRHS)) |
| 1909 | return V; |
| 1910 | if (Value *V = SimplifyOrOfICmps(ICIRHS, ICILHS)) |
| 1911 | return V; |
| 1912 | } |
| 1913 | } |
| 1914 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1915 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1916 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, |
| 1917 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1918 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1919 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1920 | // Or distributes over And. Try some generic simplifications based on this. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1921 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q, |
| 1922 | MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1923 | return V; |
| 1924 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1925 | // If the operation is with the result of a select instruction, check whether |
| 1926 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1927 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1928 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1929 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1930 | return V; |
| 1931 | |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1932 | // (A & C)|(B & D) |
| 1933 | Value *C = nullptr, *D = nullptr; |
| 1934 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 1935 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
| 1936 | ConstantInt *C1 = dyn_cast<ConstantInt>(C); |
| 1937 | ConstantInt *C2 = dyn_cast<ConstantInt>(D); |
| 1938 | if (C1 && C2 && (C1->getValue() == ~C2->getValue())) { |
| 1939 | // (A & C1)|(B & C2) |
| 1940 | // If we have: ((V + N) & C1) | (V & C2) |
| 1941 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 1942 | // replace with V+N. |
| 1943 | Value *V1, *V2; |
| 1944 | if ((C2->getValue() & (C2->getValue() + 1)) == 0 && // C2 == 0+1+ |
| 1945 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1946 | // Add commutes, try both ways. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1947 | if (V1 == B && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1948 | MaskedValueIsZero(V2, C2->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1949 | return A; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1950 | if (V2 == B && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1951 | MaskedValueIsZero(V1, C2->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1952 | return A; |
| 1953 | } |
| 1954 | // Or commutes, try both ways. |
| 1955 | if ((C1->getValue() & (C1->getValue() + 1)) == 0 && |
| 1956 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1957 | // Add commutes, try both ways. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1958 | if (V1 == A && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1959 | MaskedValueIsZero(V2, C1->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1960 | return B; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1961 | if (V2 == A && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1962 | MaskedValueIsZero(V1, C1->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1963 | return B; |
| 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1968 | // If the operation is with the result of a phi instruction, check whether |
| 1969 | // 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] | 1970 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1971 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1972 | return V; |
| 1973 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1974 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1977 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1978 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1979 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1980 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1981 | return ::SimplifyOrInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1982 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1983 | } |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1984 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1985 | /// Given operands for a Xor, see if we can fold the result. |
| 1986 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1987 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const Query &Q, |
| 1988 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1989 | if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q)) |
| 1990 | return C; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1991 | |
| 1992 | // A ^ undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1993 | if (match(Op1, m_Undef())) |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1994 | return Op1; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1995 | |
| 1996 | // A ^ 0 = A |
| 1997 | if (match(Op1, m_Zero())) |
| 1998 | return Op0; |
| 1999 | |
Eli Friedman | ad3cfe7 | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 2000 | // A ^ A = 0 |
| 2001 | if (Op0 == Op1) |
| 2002 | return Constant::getNullValue(Op0->getType()); |
| 2003 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2004 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2005 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 2006 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2007 | return Constant::getAllOnesValue(Op0->getType()); |
| 2008 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2009 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2010 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, |
| 2011 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2012 | return V; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2013 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 2014 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 2015 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 2016 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 2017 | // only if B and C are equal. If B and C are equal then (since we assume |
| 2018 | // that operands have already been simplified) "select(cond, B, C)" should |
| 2019 | // have been simplified to the common value of B and C already. Analysing |
| 2020 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 2021 | // for threading over phi nodes. |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2022 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2023 | return nullptr; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2026 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2027 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2028 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2029 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2030 | return ::SimplifyXorInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2031 | RecursionLimit); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2034 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2035 | return CmpInst::makeCmpResultType(Op->getType()); |
| 2036 | } |
| 2037 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 2038 | /// Rummage around inside V looking for something equivalent to the comparison |
| 2039 | /// "LHS Pred RHS". Return such a value if found, otherwise return null. |
| 2040 | /// Helper function for analyzing max/min idioms. |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2041 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 2042 | Value *LHS, Value *RHS) { |
| 2043 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 2044 | if (!SI) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2045 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2046 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 2047 | if (!Cmp) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2048 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2049 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 2050 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 2051 | return Cmp; |
| 2052 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 2053 | LHS == CmpRHS && RHS == CmpLHS) |
| 2054 | return Cmp; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2055 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 2058 | // A significant optimization not implemented here is assuming that alloca |
| 2059 | // addresses are not equal to incoming argument values. They don't *alias*, |
| 2060 | // as we say, but that doesn't mean they aren't equal, so we take a |
| 2061 | // conservative approach. |
| 2062 | // |
| 2063 | // This is inspired in part by C++11 5.10p1: |
| 2064 | // "Two pointers of the same type compare equal if and only if they are both |
| 2065 | // null, both point to the same function, or both represent the same |
| 2066 | // address." |
| 2067 | // |
| 2068 | // This is pretty permissive. |
| 2069 | // |
| 2070 | // It's also partly due to C11 6.5.9p6: |
| 2071 | // "Two pointers compare equal if and only if both are null pointers, both are |
| 2072 | // pointers to the same object (including a pointer to an object and a |
| 2073 | // subobject at its beginning) or function, both are pointers to one past the |
| 2074 | // last element of the same array object, or one is a pointer to one past the |
| 2075 | // 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] | 2076 | // different array object that happens to immediately follow the first array |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 2077 | // object in the address space.) |
| 2078 | // |
| 2079 | // C11's version is more restrictive, however there's no reason why an argument |
| 2080 | // couldn't be a one-past-the-end value for a stack object in the caller and be |
| 2081 | // equal to the beginning of a stack object in the callee. |
| 2082 | // |
| 2083 | // If the C and C++ standards are ever made sufficiently restrictive in this |
| 2084 | // area, it may be possible to update LLVM's semantics accordingly and reinstate |
| 2085 | // this optimization. |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2086 | static Constant * |
| 2087 | computePointerICmp(const DataLayout &DL, const TargetLibraryInfo *TLI, |
| 2088 | const DominatorTree *DT, CmpInst::Predicate Pred, |
| 2089 | const Instruction *CxtI, Value *LHS, Value *RHS) { |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2090 | // First, skip past any trivial no-ops. |
| 2091 | LHS = LHS->stripPointerCasts(); |
| 2092 | RHS = RHS->stripPointerCasts(); |
| 2093 | |
| 2094 | // A non-null pointer is not equal to a null pointer. |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2095 | if (llvm::isKnownNonNull(LHS) && isa<ConstantPointerNull>(RHS) && |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2096 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE)) |
| 2097 | return ConstantInt::get(GetCompareTy(LHS), |
| 2098 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2099 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2100 | // We can only fold certain predicates on pointer comparisons. |
| 2101 | switch (Pred) { |
| 2102 | default: |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2103 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2104 | |
| 2105 | // Equality comaprisons are easy to fold. |
| 2106 | case CmpInst::ICMP_EQ: |
| 2107 | case CmpInst::ICMP_NE: |
| 2108 | break; |
| 2109 | |
| 2110 | // We can only handle unsigned relational comparisons because 'inbounds' on |
| 2111 | // a GEP only protects against unsigned wrapping. |
| 2112 | case CmpInst::ICMP_UGT: |
| 2113 | case CmpInst::ICMP_UGE: |
| 2114 | case CmpInst::ICMP_ULT: |
| 2115 | case CmpInst::ICMP_ULE: |
| 2116 | // However, we have to switch them to their signed variants to handle |
| 2117 | // negative indices from the base pointer. |
| 2118 | Pred = ICmpInst::getSignedPredicate(Pred); |
| 2119 | break; |
| 2120 | } |
| 2121 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2122 | // Strip off any constant offsets so that we can reason about them. |
| 2123 | // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets |
| 2124 | // here and compare base addresses like AliasAnalysis does, however there are |
| 2125 | // numerous hazards. AliasAnalysis and its utilities rely on special rules |
| 2126 | // governing loads and stores which don't apply to icmps. Also, AliasAnalysis |
| 2127 | // doesn't need to guarantee pointer inequality when it says NoAlias. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2128 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 2129 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2130 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2131 | // If LHS and RHS are related via constant offsets to the same base |
| 2132 | // value, we can replace it with an icmp which just compares the offsets. |
| 2133 | if (LHS == RHS) |
| 2134 | return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2135 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2136 | // Various optimizations for (in)equality comparisons. |
| 2137 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) { |
| 2138 | // Different non-empty allocations that exist at the same time have |
| 2139 | // different addresses (if the program can tell). Global variables always |
| 2140 | // exist, so they always exist during the lifetime of each other and all |
| 2141 | // allocas. Two different allocas usually have different addresses... |
| 2142 | // |
| 2143 | // However, if there's an @llvm.stackrestore dynamically in between two |
| 2144 | // allocas, they may have the same address. It's tempting to reduce the |
| 2145 | // scope of the problem by only looking at *static* allocas here. That would |
| 2146 | // cover the majority of allocas while significantly reducing the likelihood |
| 2147 | // of having an @llvm.stackrestore pop up in the middle. However, it's not |
| 2148 | // actually impossible for an @llvm.stackrestore to pop up in the middle of |
| 2149 | // an entry block. Also, if we have a block that's not attached to a |
| 2150 | // function, we can't tell if it's "static" under the current definition. |
| 2151 | // Theoretically, this problem could be fixed by creating a new kind of |
| 2152 | // instruction kind specifically for static allocas. Such a new instruction |
| 2153 | // could be required to be at the top of the entry block, thus preventing it |
| 2154 | // from being subject to a @llvm.stackrestore. Instcombine could even |
| 2155 | // convert regular allocas into these special allocas. It'd be nifty. |
| 2156 | // However, until then, this problem remains open. |
| 2157 | // |
| 2158 | // So, we'll assume that two non-empty allocas have different addresses |
| 2159 | // for now. |
| 2160 | // |
| 2161 | // With all that, if the offsets are within the bounds of their allocations |
| 2162 | // (and not one-past-the-end! so we can't use inbounds!), and their |
| 2163 | // allocations aren't the same, the pointers are not equal. |
| 2164 | // |
| 2165 | // Note that it's not necessary to check for LHS being a global variable |
| 2166 | // address, due to canonicalization and constant folding. |
| 2167 | if (isa<AllocaInst>(LHS) && |
| 2168 | (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2169 | ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset); |
| 2170 | ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2171 | uint64_t LHSSize, RHSSize; |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2172 | if (LHSOffsetCI && RHSOffsetCI && |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2173 | getObjectSize(LHS, LHSSize, DL, TLI) && |
| 2174 | getObjectSize(RHS, RHSSize, DL, TLI)) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2175 | const APInt &LHSOffsetValue = LHSOffsetCI->getValue(); |
| 2176 | const APInt &RHSOffsetValue = RHSOffsetCI->getValue(); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2177 | if (!LHSOffsetValue.isNegative() && |
| 2178 | !RHSOffsetValue.isNegative() && |
| 2179 | LHSOffsetValue.ult(LHSSize) && |
| 2180 | RHSOffsetValue.ult(RHSSize)) { |
| 2181 | return ConstantInt::get(GetCompareTy(LHS), |
| 2182 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | // Repeat the above check but this time without depending on DataLayout |
| 2187 | // or being able to compute a precise size. |
| 2188 | if (!cast<PointerType>(LHS->getType())->isEmptyTy() && |
| 2189 | !cast<PointerType>(RHS->getType())->isEmptyTy() && |
| 2190 | LHSOffset->isNullValue() && |
| 2191 | RHSOffset->isNullValue()) |
| 2192 | return ConstantInt::get(GetCompareTy(LHS), |
| 2193 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2194 | } |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2195 | |
| 2196 | // Even if an non-inbounds GEP occurs along the path we can still optimize |
| 2197 | // equality comparisons concerning the result. We avoid walking the whole |
| 2198 | // chain again by starting where the last calls to |
| 2199 | // stripAndComputeConstantOffsets left off and accumulate the offsets. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2200 | Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true); |
| 2201 | Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true); |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2202 | if (LHS == RHS) |
| 2203 | return ConstantExpr::getICmp(Pred, |
| 2204 | ConstantExpr::getAdd(LHSOffset, LHSNoBound), |
| 2205 | ConstantExpr::getAdd(RHSOffset, RHSNoBound)); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2206 | |
| 2207 | // If one side of the equality comparison must come from a noalias call |
| 2208 | // (meaning a system memory allocation function), and the other side must |
| 2209 | // come from a pointer that cannot overlap with dynamically-allocated |
| 2210 | // memory within the lifetime of the current function (allocas, byval |
| 2211 | // arguments, globals), then determine the comparison result here. |
| 2212 | SmallVector<Value *, 8> LHSUObjs, RHSUObjs; |
| 2213 | GetUnderlyingObjects(LHS, LHSUObjs, DL); |
| 2214 | GetUnderlyingObjects(RHS, RHSUObjs, DL); |
| 2215 | |
| 2216 | // Is the set of underlying objects all noalias calls? |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2217 | auto IsNAC = [](ArrayRef<Value *> Objects) { |
| 2218 | return all_of(Objects, isNoAliasCall); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2219 | }; |
| 2220 | |
| 2221 | // Is the set of underlying objects all things which must be disjoint from |
Hal Finkel | aa19baf | 2014-12-04 17:45:19 +0000 | [diff] [blame] | 2222 | // noalias calls. For allocas, we consider only static ones (dynamic |
| 2223 | // allocas might be transformed into calls to malloc not simultaneously |
| 2224 | // live with the compared-to allocation). For globals, we exclude symbols |
| 2225 | // that might be resolve lazily to symbols in another dynamically-loaded |
| 2226 | // library (and, thus, could be malloc'ed by the implementation). |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2227 | auto IsAllocDisjoint = [](ArrayRef<Value *> Objects) { |
| 2228 | return all_of(Objects, [](Value *V) { |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2229 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) |
| 2230 | return AI->getParent() && AI->getFunction() && AI->isStaticAlloca(); |
| 2231 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 2232 | return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() || |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 2233 | GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) && |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2234 | !GV->isThreadLocal(); |
| 2235 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 2236 | return A->hasByValAttr(); |
| 2237 | return false; |
| 2238 | }); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2239 | }; |
| 2240 | |
| 2241 | if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) || |
| 2242 | (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs))) |
| 2243 | return ConstantInt::get(GetCompareTy(LHS), |
| 2244 | !CmpInst::isTrueWhenEqual(Pred)); |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2245 | |
| 2246 | // Fold comparisons for non-escaping pointer even if the allocation call |
| 2247 | // cannot be elided. We cannot fold malloc comparison to null. Also, the |
| 2248 | // dynamic allocation call could be either of the operands. |
| 2249 | Value *MI = nullptr; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2250 | if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonNullAt(RHS, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2251 | MI = LHS; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2252 | else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonNullAt(LHS, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2253 | MI = RHS; |
| 2254 | // FIXME: We should also fold the compare when the pointer escapes, but the |
| 2255 | // compare dominates the pointer escape |
| 2256 | if (MI && !PointerMayBeCaptured(MI, true, true)) |
| 2257 | return ConstantInt::get(GetCompareTy(LHS), |
| 2258 | CmpInst::isFalseWhenEqual(Pred)); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | // Otherwise, fail. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2262 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2263 | } |
Chris Lattner | 01990f0 | 2012-02-24 19:01:58 +0000 | [diff] [blame] | 2264 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2265 | /// Fold an icmp when its operands have i1 scalar type. |
| 2266 | static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS, |
| 2267 | Value *RHS, const Query &Q) { |
| 2268 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2269 | Type *OpTy = LHS->getType(); // The operand type. |
| 2270 | if (!OpTy->getScalarType()->isIntegerTy(1)) |
| 2271 | return nullptr; |
| 2272 | |
| 2273 | switch (Pred) { |
| 2274 | default: |
| 2275 | break; |
| 2276 | case ICmpInst::ICMP_EQ: |
| 2277 | // X == 1 -> X |
| 2278 | if (match(RHS, m_One())) |
| 2279 | return LHS; |
| 2280 | break; |
| 2281 | case ICmpInst::ICMP_NE: |
| 2282 | // X != 0 -> X |
| 2283 | if (match(RHS, m_Zero())) |
| 2284 | return LHS; |
| 2285 | break; |
| 2286 | case ICmpInst::ICMP_UGT: |
| 2287 | // X >u 0 -> X |
| 2288 | if (match(RHS, m_Zero())) |
| 2289 | return LHS; |
| 2290 | break; |
| 2291 | case ICmpInst::ICMP_UGE: |
| 2292 | // X >=u 1 -> X |
| 2293 | if (match(RHS, m_One())) |
| 2294 | return LHS; |
| 2295 | if (isImpliedCondition(RHS, LHS, Q.DL).getValueOr(false)) |
| 2296 | return getTrue(ITy); |
| 2297 | break; |
| 2298 | case ICmpInst::ICMP_SGE: |
| 2299 | /// For signed comparison, the values for an i1 are 0 and -1 |
| 2300 | /// respectively. This maps into a truth table of: |
| 2301 | /// LHS | RHS | LHS >=s RHS | LHS implies RHS |
| 2302 | /// 0 | 0 | 1 (0 >= 0) | 1 |
| 2303 | /// 0 | 1 | 1 (0 >= -1) | 1 |
| 2304 | /// 1 | 0 | 0 (-1 >= 0) | 0 |
| 2305 | /// 1 | 1 | 1 (-1 >= -1) | 1 |
| 2306 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
| 2307 | return getTrue(ITy); |
| 2308 | break; |
| 2309 | case ICmpInst::ICMP_SLT: |
| 2310 | // X <s 0 -> X |
| 2311 | if (match(RHS, m_Zero())) |
| 2312 | return LHS; |
| 2313 | break; |
| 2314 | case ICmpInst::ICMP_SLE: |
| 2315 | // X <=s -1 -> X |
| 2316 | if (match(RHS, m_One())) |
| 2317 | return LHS; |
| 2318 | break; |
| 2319 | case ICmpInst::ICMP_ULE: |
| 2320 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
| 2321 | return getTrue(ITy); |
| 2322 | break; |
| 2323 | } |
| 2324 | |
| 2325 | return nullptr; |
| 2326 | } |
| 2327 | |
| 2328 | /// Try hard to fold icmp with zero RHS because this is a common case. |
| 2329 | static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS, |
| 2330 | Value *RHS, const Query &Q) { |
| 2331 | if (!match(RHS, m_Zero())) |
| 2332 | return nullptr; |
| 2333 | |
| 2334 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2335 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 2336 | switch (Pred) { |
| 2337 | default: |
| 2338 | llvm_unreachable("Unknown ICmp predicate!"); |
| 2339 | case ICmpInst::ICMP_ULT: |
| 2340 | return getFalse(ITy); |
| 2341 | case ICmpInst::ICMP_UGE: |
| 2342 | return getTrue(ITy); |
| 2343 | case ICmpInst::ICMP_EQ: |
| 2344 | case ICmpInst::ICMP_ULE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2345 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2346 | return getFalse(ITy); |
| 2347 | break; |
| 2348 | case ICmpInst::ICMP_NE: |
| 2349 | case ICmpInst::ICMP_UGT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2350 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2351 | return getTrue(ITy); |
| 2352 | break; |
| 2353 | case ICmpInst::ICMP_SLT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2354 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2355 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2356 | if (LHSKnownNegative) |
| 2357 | return getTrue(ITy); |
| 2358 | if (LHSKnownNonNegative) |
| 2359 | return getFalse(ITy); |
| 2360 | break; |
| 2361 | case ICmpInst::ICMP_SLE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2362 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2363 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2364 | if (LHSKnownNegative) |
| 2365 | return getTrue(ITy); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2366 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2367 | return getFalse(ITy); |
| 2368 | break; |
| 2369 | case ICmpInst::ICMP_SGE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2370 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2371 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2372 | if (LHSKnownNegative) |
| 2373 | return getFalse(ITy); |
| 2374 | if (LHSKnownNonNegative) |
| 2375 | return getTrue(ITy); |
| 2376 | break; |
| 2377 | case ICmpInst::ICMP_SGT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2378 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2379 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2380 | if (LHSKnownNegative) |
| 2381 | return getFalse(ITy); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2382 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2383 | return getTrue(ITy); |
| 2384 | break; |
| 2385 | } |
| 2386 | |
| 2387 | return nullptr; |
| 2388 | } |
| 2389 | |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2390 | /// Many binary operators with a constant operand have an easy-to-compute |
| 2391 | /// range of outputs. This can be used to fold a comparison to always true or |
| 2392 | /// always false. |
| 2393 | static void setLimitsForBinOp(BinaryOperator &BO, APInt &Lower, APInt &Upper) { |
| 2394 | unsigned Width = Lower.getBitWidth(); |
| 2395 | const APInt *C; |
| 2396 | switch (BO.getOpcode()) { |
| 2397 | case Instruction::Add: |
Sanjay Patel | 5622725 | 2017-01-24 17:03:24 +0000 | [diff] [blame] | 2398 | if (match(BO.getOperand(1), m_APInt(C)) && *C != 0) { |
| 2399 | // FIXME: If we have both nuw and nsw, we should reduce the range further. |
| 2400 | if (BO.hasNoUnsignedWrap()) { |
| 2401 | // 'add nuw x, C' produces [C, UINT_MAX]. |
| 2402 | Lower = *C; |
| 2403 | } else if (BO.hasNoSignedWrap()) { |
| 2404 | if (C->isNegative()) { |
| 2405 | // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. |
| 2406 | Lower = APInt::getSignedMinValue(Width); |
| 2407 | Upper = APInt::getSignedMaxValue(Width) + *C + 1; |
| 2408 | } else { |
| 2409 | // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. |
| 2410 | Lower = APInt::getSignedMinValue(Width) + *C; |
| 2411 | Upper = APInt::getSignedMaxValue(Width) + 1; |
| 2412 | } |
| 2413 | } |
| 2414 | } |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2415 | break; |
| 2416 | |
| 2417 | case Instruction::And: |
| 2418 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2419 | // 'and x, C' produces [0, C]. |
| 2420 | Upper = *C + 1; |
| 2421 | break; |
| 2422 | |
| 2423 | case Instruction::Or: |
| 2424 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2425 | // 'or x, C' produces [C, UINT_MAX]. |
| 2426 | Lower = *C; |
| 2427 | break; |
| 2428 | |
| 2429 | case Instruction::AShr: |
| 2430 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 2431 | // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. |
| 2432 | Lower = APInt::getSignedMinValue(Width).ashr(*C); |
| 2433 | Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1; |
| 2434 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2435 | unsigned ShiftAmount = Width - 1; |
| 2436 | if (*C != 0 && BO.isExact()) |
| 2437 | ShiftAmount = C->countTrailingZeros(); |
| 2438 | if (C->isNegative()) { |
| 2439 | // 'ashr C, x' produces [C, C >> (Width-1)] |
| 2440 | Lower = *C; |
| 2441 | Upper = C->ashr(ShiftAmount) + 1; |
| 2442 | } else { |
| 2443 | // 'ashr C, x' produces [C >> (Width-1), C] |
| 2444 | Lower = C->ashr(ShiftAmount); |
| 2445 | Upper = *C + 1; |
| 2446 | } |
| 2447 | } |
| 2448 | break; |
| 2449 | |
| 2450 | case Instruction::LShr: |
| 2451 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 2452 | // 'lshr x, C' produces [0, UINT_MAX >> C]. |
| 2453 | Upper = APInt::getAllOnesValue(Width).lshr(*C) + 1; |
| 2454 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2455 | // 'lshr C, x' produces [C >> (Width-1), C]. |
| 2456 | unsigned ShiftAmount = Width - 1; |
| 2457 | if (*C != 0 && BO.isExact()) |
| 2458 | ShiftAmount = C->countTrailingZeros(); |
| 2459 | Lower = C->lshr(ShiftAmount); |
| 2460 | Upper = *C + 1; |
| 2461 | } |
| 2462 | break; |
| 2463 | |
| 2464 | case Instruction::Shl: |
| 2465 | if (match(BO.getOperand(0), m_APInt(C))) { |
| 2466 | if (BO.hasNoUnsignedWrap()) { |
| 2467 | // 'shl nuw C, x' produces [C, C << CLZ(C)] |
| 2468 | Lower = *C; |
| 2469 | Upper = Lower.shl(Lower.countLeadingZeros()) + 1; |
| 2470 | } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw? |
| 2471 | if (C->isNegative()) { |
| 2472 | // 'shl nsw C, x' produces [C << CLO(C)-1, C] |
| 2473 | unsigned ShiftAmount = C->countLeadingOnes() - 1; |
| 2474 | Lower = C->shl(ShiftAmount); |
| 2475 | Upper = *C + 1; |
| 2476 | } else { |
| 2477 | // 'shl nsw C, x' produces [C, C << CLZ(C)-1] |
| 2478 | unsigned ShiftAmount = C->countLeadingZeros() - 1; |
| 2479 | Lower = *C; |
| 2480 | Upper = C->shl(ShiftAmount) + 1; |
| 2481 | } |
| 2482 | } |
| 2483 | } |
| 2484 | break; |
| 2485 | |
| 2486 | case Instruction::SDiv: |
| 2487 | if (match(BO.getOperand(1), m_APInt(C))) { |
| 2488 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2489 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 2490 | if (C->isAllOnesValue()) { |
| 2491 | // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] |
| 2492 | // where C != -1 and C != 0 and C != 1 |
| 2493 | Lower = IntMin + 1; |
| 2494 | Upper = IntMax + 1; |
| 2495 | } else if (C->countLeadingZeros() < Width - 1) { |
| 2496 | // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] |
| 2497 | // where C != -1 and C != 0 and C != 1 |
| 2498 | Lower = IntMin.sdiv(*C); |
| 2499 | Upper = IntMax.sdiv(*C); |
| 2500 | if (Lower.sgt(Upper)) |
| 2501 | std::swap(Lower, Upper); |
| 2502 | Upper = Upper + 1; |
| 2503 | assert(Upper != Lower && "Upper part of range has wrapped!"); |
| 2504 | } |
| 2505 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2506 | if (C->isMinSignedValue()) { |
| 2507 | // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. |
| 2508 | Lower = *C; |
| 2509 | Upper = Lower.lshr(1) + 1; |
| 2510 | } else { |
| 2511 | // 'sdiv C, x' produces [-|C|, |C|]. |
| 2512 | Upper = C->abs() + 1; |
| 2513 | Lower = (-Upper) + 1; |
| 2514 | } |
| 2515 | } |
| 2516 | break; |
| 2517 | |
| 2518 | case Instruction::UDiv: |
| 2519 | if (match(BO.getOperand(1), m_APInt(C)) && *C != 0) { |
| 2520 | // 'udiv x, C' produces [0, UINT_MAX / C]. |
| 2521 | Upper = APInt::getMaxValue(Width).udiv(*C) + 1; |
| 2522 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2523 | // 'udiv C, x' produces [0, C]. |
| 2524 | Upper = *C + 1; |
| 2525 | } |
| 2526 | break; |
| 2527 | |
| 2528 | case Instruction::SRem: |
| 2529 | if (match(BO.getOperand(1), m_APInt(C))) { |
| 2530 | // 'srem x, C' produces (-|C|, |C|). |
| 2531 | Upper = C->abs(); |
| 2532 | Lower = (-Upper) + 1; |
| 2533 | } |
| 2534 | break; |
| 2535 | |
| 2536 | case Instruction::URem: |
| 2537 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2538 | // 'urem x, C' produces [0, C). |
| 2539 | Upper = *C; |
| 2540 | break; |
| 2541 | |
| 2542 | default: |
| 2543 | break; |
| 2544 | } |
| 2545 | } |
| 2546 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2547 | static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS, |
| 2548 | Value *RHS) { |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2549 | const APInt *C; |
| 2550 | if (!match(RHS, m_APInt(C))) |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2551 | return nullptr; |
| 2552 | |
| 2553 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
Sanjoy Das | 1f7b813 | 2016-10-02 00:09:57 +0000 | [diff] [blame] | 2554 | ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2555 | if (RHS_CR.isEmptySet()) |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2556 | return ConstantInt::getFalse(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2557 | if (RHS_CR.isFullSet()) |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2558 | return ConstantInt::getTrue(GetCompareTy(RHS)); |
| 2559 | |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2560 | // Find the range of possible values for binary operators. |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2561 | unsigned Width = C->getBitWidth(); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2562 | APInt Lower = APInt(Width, 0); |
| 2563 | APInt Upper = APInt(Width, 0); |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2564 | if (auto *BO = dyn_cast<BinaryOperator>(LHS)) |
| 2565 | setLimitsForBinOp(*BO, Lower, Upper); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2566 | |
| 2567 | ConstantRange LHS_CR = |
| 2568 | Lower != Upper ? ConstantRange(Lower, Upper) : ConstantRange(Width, true); |
| 2569 | |
| 2570 | if (auto *I = dyn_cast<Instruction>(LHS)) |
| 2571 | if (auto *Ranges = I->getMetadata(LLVMContext::MD_range)) |
| 2572 | LHS_CR = LHS_CR.intersectWith(getConstantRangeFromMetadata(*Ranges)); |
| 2573 | |
| 2574 | if (!LHS_CR.isFullSet()) { |
| 2575 | if (RHS_CR.contains(LHS_CR)) |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2576 | return ConstantInt::getTrue(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2577 | if (RHS_CR.inverse().contains(LHS_CR)) |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2578 | return ConstantInt::getFalse(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | return nullptr; |
| 2582 | } |
| 2583 | |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2584 | static Value *simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS, |
| 2585 | Value *RHS, const Query &Q, |
| 2586 | unsigned MaxRecurse) { |
| 2587 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2588 | |
| 2589 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 2590 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 2591 | if (MaxRecurse && (LBO || RBO)) { |
| 2592 | // Analyze the case when either LHS or RHS is an add instruction. |
| 2593 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
| 2594 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 2595 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 2596 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 2597 | A = LBO->getOperand(0); |
| 2598 | B = LBO->getOperand(1); |
| 2599 | NoLHSWrapProblem = |
| 2600 | ICmpInst::isEquality(Pred) || |
| 2601 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 2602 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 2603 | } |
| 2604 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 2605 | C = RBO->getOperand(0); |
| 2606 | D = RBO->getOperand(1); |
| 2607 | NoRHSWrapProblem = |
| 2608 | ICmpInst::isEquality(Pred) || |
| 2609 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 2610 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 2611 | } |
| 2612 | |
| 2613 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2614 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 2615 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 2616 | Constant::getNullValue(RHS->getType()), Q, |
| 2617 | MaxRecurse - 1)) |
| 2618 | return V; |
| 2619 | |
| 2620 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2621 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 2622 | if (Value *V = |
| 2623 | SimplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()), |
| 2624 | C == LHS ? D : C, Q, MaxRecurse - 1)) |
| 2625 | return V; |
| 2626 | |
| 2627 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 2628 | if (A && C && (A == C || A == D || B == C || B == D) && NoLHSWrapProblem && |
| 2629 | NoRHSWrapProblem) { |
| 2630 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
| 2631 | Value *Y, *Z; |
| 2632 | if (A == C) { |
| 2633 | // C + B == C + D -> B == D |
| 2634 | Y = B; |
| 2635 | Z = D; |
| 2636 | } else if (A == D) { |
| 2637 | // D + B == C + D -> B == C |
| 2638 | Y = B; |
| 2639 | Z = C; |
| 2640 | } else if (B == C) { |
| 2641 | // A + C == C + D -> A == D |
| 2642 | Y = A; |
| 2643 | Z = D; |
| 2644 | } else { |
| 2645 | assert(B == D); |
| 2646 | // A + D == C + D -> A == C |
| 2647 | Y = A; |
| 2648 | Z = C; |
| 2649 | } |
| 2650 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1)) |
| 2651 | return V; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | { |
| 2656 | Value *Y = nullptr; |
| 2657 | // icmp pred (or X, Y), X |
| 2658 | if (LBO && match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) { |
| 2659 | if (Pred == ICmpInst::ICMP_ULT) |
| 2660 | return getFalse(ITy); |
| 2661 | if (Pred == ICmpInst::ICMP_UGE) |
| 2662 | return getTrue(ITy); |
| 2663 | |
| 2664 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) { |
| 2665 | bool RHSKnownNonNegative, RHSKnownNegative; |
| 2666 | bool YKnownNonNegative, YKnownNegative; |
| 2667 | ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, Q.DL, 0, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2668 | Q.AC, Q.CxtI, Q.DT); |
| 2669 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC, |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2670 | Q.CxtI, Q.DT); |
| 2671 | if (RHSKnownNonNegative && YKnownNegative) |
| 2672 | return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy); |
| 2673 | if (RHSKnownNegative || YKnownNonNegative) |
| 2674 | return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy); |
| 2675 | } |
| 2676 | } |
| 2677 | // icmp pred X, (or X, Y) |
| 2678 | if (RBO && match(RBO, m_c_Or(m_Value(Y), m_Specific(LHS)))) { |
| 2679 | if (Pred == ICmpInst::ICMP_ULE) |
| 2680 | return getTrue(ITy); |
| 2681 | if (Pred == ICmpInst::ICMP_UGT) |
| 2682 | return getFalse(ITy); |
| 2683 | |
| 2684 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLE) { |
| 2685 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 2686 | bool YKnownNonNegative, YKnownNegative; |
| 2687 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2688 | Q.AC, Q.CxtI, Q.DT); |
| 2689 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC, |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2690 | Q.CxtI, Q.DT); |
| 2691 | if (LHSKnownNonNegative && YKnownNegative) |
| 2692 | return Pred == ICmpInst::ICMP_SGT ? getTrue(ITy) : getFalse(ITy); |
| 2693 | if (LHSKnownNegative || YKnownNonNegative) |
| 2694 | return Pred == ICmpInst::ICMP_SGT ? getFalse(ITy) : getTrue(ITy); |
| 2695 | } |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | // icmp pred (and X, Y), X |
| 2700 | if (LBO && match(LBO, m_CombineOr(m_And(m_Value(), m_Specific(RHS)), |
| 2701 | m_And(m_Specific(RHS), m_Value())))) { |
| 2702 | if (Pred == ICmpInst::ICMP_UGT) |
| 2703 | return getFalse(ITy); |
| 2704 | if (Pred == ICmpInst::ICMP_ULE) |
| 2705 | return getTrue(ITy); |
| 2706 | } |
| 2707 | // icmp pred X, (and X, Y) |
| 2708 | if (RBO && match(RBO, m_CombineOr(m_And(m_Value(), m_Specific(LHS)), |
| 2709 | m_And(m_Specific(LHS), m_Value())))) { |
| 2710 | if (Pred == ICmpInst::ICMP_UGE) |
| 2711 | return getTrue(ITy); |
| 2712 | if (Pred == ICmpInst::ICMP_ULT) |
| 2713 | return getFalse(ITy); |
| 2714 | } |
| 2715 | |
| 2716 | // 0 - (zext X) pred C |
| 2717 | if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) { |
| 2718 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2719 | if (RHSC->getValue().isStrictlyPositive()) { |
| 2720 | if (Pred == ICmpInst::ICMP_SLT) |
| 2721 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2722 | if (Pred == ICmpInst::ICMP_SGE) |
| 2723 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2724 | if (Pred == ICmpInst::ICMP_EQ) |
| 2725 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2726 | if (Pred == ICmpInst::ICMP_NE) |
| 2727 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2728 | } |
| 2729 | if (RHSC->getValue().isNonNegative()) { |
| 2730 | if (Pred == ICmpInst::ICMP_SLE) |
| 2731 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2732 | if (Pred == ICmpInst::ICMP_SGT) |
| 2733 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2734 | } |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | // icmp pred (urem X, Y), Y |
| 2739 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
| 2740 | bool KnownNonNegative, KnownNegative; |
| 2741 | switch (Pred) { |
| 2742 | default: |
| 2743 | break; |
| 2744 | case ICmpInst::ICMP_SGT: |
| 2745 | case ICmpInst::ICMP_SGE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2746 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2747 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2748 | if (!KnownNonNegative) |
| 2749 | break; |
| 2750 | LLVM_FALLTHROUGH; |
| 2751 | case ICmpInst::ICMP_EQ: |
| 2752 | case ICmpInst::ICMP_UGT: |
| 2753 | case ICmpInst::ICMP_UGE: |
| 2754 | return getFalse(ITy); |
| 2755 | case ICmpInst::ICMP_SLT: |
| 2756 | case ICmpInst::ICMP_SLE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2757 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2758 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2759 | if (!KnownNonNegative) |
| 2760 | break; |
| 2761 | LLVM_FALLTHROUGH; |
| 2762 | case ICmpInst::ICMP_NE: |
| 2763 | case ICmpInst::ICMP_ULT: |
| 2764 | case ICmpInst::ICMP_ULE: |
| 2765 | return getTrue(ITy); |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | // icmp pred X, (urem Y, X) |
| 2770 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
| 2771 | bool KnownNonNegative, KnownNegative; |
| 2772 | switch (Pred) { |
| 2773 | default: |
| 2774 | break; |
| 2775 | case ICmpInst::ICMP_SGT: |
| 2776 | case ICmpInst::ICMP_SGE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2777 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2778 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2779 | if (!KnownNonNegative) |
| 2780 | break; |
| 2781 | LLVM_FALLTHROUGH; |
| 2782 | case ICmpInst::ICMP_NE: |
| 2783 | case ICmpInst::ICMP_UGT: |
| 2784 | case ICmpInst::ICMP_UGE: |
| 2785 | return getTrue(ITy); |
| 2786 | case ICmpInst::ICMP_SLT: |
| 2787 | case ICmpInst::ICMP_SLE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2788 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2789 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2790 | if (!KnownNonNegative) |
| 2791 | break; |
| 2792 | LLVM_FALLTHROUGH; |
| 2793 | case ICmpInst::ICMP_EQ: |
| 2794 | case ICmpInst::ICMP_ULT: |
| 2795 | case ICmpInst::ICMP_ULE: |
| 2796 | return getFalse(ITy); |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | // x >> y <=u x |
| 2801 | // x udiv y <=u x. |
| 2802 | if (LBO && (match(LBO, m_LShr(m_Specific(RHS), m_Value())) || |
| 2803 | match(LBO, m_UDiv(m_Specific(RHS), m_Value())))) { |
| 2804 | // icmp pred (X op Y), X |
| 2805 | if (Pred == ICmpInst::ICMP_UGT) |
| 2806 | return getFalse(ITy); |
| 2807 | if (Pred == ICmpInst::ICMP_ULE) |
| 2808 | return getTrue(ITy); |
| 2809 | } |
| 2810 | |
| 2811 | // x >=u x >> y |
| 2812 | // x >=u x udiv y. |
| 2813 | if (RBO && (match(RBO, m_LShr(m_Specific(LHS), m_Value())) || |
| 2814 | match(RBO, m_UDiv(m_Specific(LHS), m_Value())))) { |
| 2815 | // icmp pred X, (X op Y) |
| 2816 | if (Pred == ICmpInst::ICMP_ULT) |
| 2817 | return getFalse(ITy); |
| 2818 | if (Pred == ICmpInst::ICMP_UGE) |
| 2819 | return getTrue(ITy); |
| 2820 | } |
| 2821 | |
| 2822 | // handle: |
| 2823 | // CI2 << X == CI |
| 2824 | // CI2 << X != CI |
| 2825 | // |
| 2826 | // where CI2 is a power of 2 and CI isn't |
| 2827 | if (auto *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2828 | const APInt *CI2Val, *CIVal = &CI->getValue(); |
| 2829 | if (LBO && match(LBO, m_Shl(m_APInt(CI2Val), m_Value())) && |
| 2830 | CI2Val->isPowerOf2()) { |
| 2831 | if (!CIVal->isPowerOf2()) { |
| 2832 | // CI2 << X can equal zero in some circumstances, |
| 2833 | // this simplification is unsafe if CI is zero. |
| 2834 | // |
| 2835 | // We know it is safe if: |
| 2836 | // - The shift is nsw, we can't shift out the one bit. |
| 2837 | // - The shift is nuw, we can't shift out the one bit. |
| 2838 | // - CI2 is one |
| 2839 | // - CI isn't zero |
| 2840 | if (LBO->hasNoSignedWrap() || LBO->hasNoUnsignedWrap() || |
| 2841 | *CI2Val == 1 || !CI->isZero()) { |
| 2842 | if (Pred == ICmpInst::ICMP_EQ) |
| 2843 | return ConstantInt::getFalse(RHS->getContext()); |
| 2844 | if (Pred == ICmpInst::ICMP_NE) |
| 2845 | return ConstantInt::getTrue(RHS->getContext()); |
| 2846 | } |
| 2847 | } |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 2848 | if (CIVal->isSignMask() && *CI2Val == 1) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2849 | if (Pred == ICmpInst::ICMP_UGT) |
| 2850 | return ConstantInt::getFalse(RHS->getContext()); |
| 2851 | if (Pred == ICmpInst::ICMP_ULE) |
| 2852 | return ConstantInt::getTrue(RHS->getContext()); |
| 2853 | } |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 2858 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 2859 | switch (LBO->getOpcode()) { |
| 2860 | default: |
| 2861 | break; |
| 2862 | case Instruction::UDiv: |
| 2863 | case Instruction::LShr: |
| 2864 | if (ICmpInst::isSigned(Pred)) |
| 2865 | break; |
| 2866 | LLVM_FALLTHROUGH; |
| 2867 | case Instruction::SDiv: |
| 2868 | case Instruction::AShr: |
| 2869 | if (!LBO->isExact() || !RBO->isExact()) |
| 2870 | break; |
| 2871 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2872 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2873 | return V; |
| 2874 | break; |
| 2875 | case Instruction::Shl: { |
| 2876 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
| 2877 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2878 | if (!NUW && !NSW) |
| 2879 | break; |
| 2880 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2881 | break; |
| 2882 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2883 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2884 | return V; |
| 2885 | break; |
| 2886 | } |
| 2887 | } |
| 2888 | } |
| 2889 | return nullptr; |
| 2890 | } |
| 2891 | |
Sanjay Patel | 35289c6 | 2016-12-10 17:40:47 +0000 | [diff] [blame] | 2892 | /// Simplify integer comparisons where at least one operand of the compare |
| 2893 | /// matches an integer min/max idiom. |
| 2894 | static Value *simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS, |
| 2895 | Value *RHS, const Query &Q, |
| 2896 | unsigned MaxRecurse) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2897 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2898 | Value *A, *B; |
| 2899 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
| 2900 | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
| 2901 | |
| 2902 | // Signed variants on "max(a,b)>=a -> true". |
| 2903 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2904 | if (A != RHS) |
| 2905 | std::swap(A, B); // smax(A, B) pred A. |
| 2906 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2907 | // We analyze this as smax(A, B) pred A. |
| 2908 | P = Pred; |
| 2909 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2910 | (A == LHS || B == LHS)) { |
| 2911 | if (A != LHS) |
| 2912 | std::swap(A, B); // A pred smax(A, B). |
| 2913 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2914 | // We analyze this as smax(A, B) swapped-pred A. |
| 2915 | P = CmpInst::getSwappedPredicate(Pred); |
| 2916 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2917 | (A == RHS || B == RHS)) { |
| 2918 | if (A != RHS) |
| 2919 | std::swap(A, B); // smin(A, B) pred A. |
| 2920 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2921 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2922 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2923 | P = CmpInst::getSwappedPredicate(Pred); |
| 2924 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2925 | (A == LHS || B == LHS)) { |
| 2926 | if (A != LHS) |
| 2927 | std::swap(A, B); // A pred smin(A, B). |
| 2928 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2929 | // We analyze this as smax(-A, -B) pred -A. |
| 2930 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2931 | P = Pred; |
| 2932 | } |
| 2933 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2934 | // Cases correspond to "max(A, B) p A". |
| 2935 | switch (P) { |
| 2936 | default: |
| 2937 | break; |
| 2938 | case CmpInst::ICMP_EQ: |
| 2939 | case CmpInst::ICMP_SLE: |
| 2940 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2941 | // in the max/min; if so, we can just return that. |
| 2942 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2943 | return V; |
| 2944 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2945 | return V; |
| 2946 | // Otherwise, see if "A EqP B" simplifies. |
| 2947 | if (MaxRecurse) |
| 2948 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
| 2949 | return V; |
| 2950 | break; |
| 2951 | case CmpInst::ICMP_NE: |
| 2952 | case CmpInst::ICMP_SGT: { |
| 2953 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2954 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2955 | // tested in the max/min; if so, we can just return that. |
| 2956 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2957 | return V; |
| 2958 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2959 | return V; |
| 2960 | // Otherwise, see if "A InvEqP B" simplifies. |
| 2961 | if (MaxRecurse) |
| 2962 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
| 2963 | return V; |
| 2964 | break; |
| 2965 | } |
| 2966 | case CmpInst::ICMP_SGE: |
| 2967 | // Always true. |
| 2968 | return getTrue(ITy); |
| 2969 | case CmpInst::ICMP_SLT: |
| 2970 | // Always false. |
| 2971 | return getFalse(ITy); |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | // Unsigned variants on "max(a,b)>=a -> true". |
| 2976 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2977 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2978 | if (A != RHS) |
| 2979 | std::swap(A, B); // umax(A, B) pred A. |
| 2980 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2981 | // We analyze this as umax(A, B) pred A. |
| 2982 | P = Pred; |
| 2983 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2984 | (A == LHS || B == LHS)) { |
| 2985 | if (A != LHS) |
| 2986 | std::swap(A, B); // A pred umax(A, B). |
| 2987 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2988 | // We analyze this as umax(A, B) swapped-pred A. |
| 2989 | P = CmpInst::getSwappedPredicate(Pred); |
| 2990 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2991 | (A == RHS || B == RHS)) { |
| 2992 | if (A != RHS) |
| 2993 | std::swap(A, B); // umin(A, B) pred A. |
| 2994 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2995 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 2996 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2997 | P = CmpInst::getSwappedPredicate(Pred); |
| 2998 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2999 | (A == LHS || B == LHS)) { |
| 3000 | if (A != LHS) |
| 3001 | std::swap(A, B); // A pred umin(A, B). |
| 3002 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 3003 | // We analyze this as umax(-A, -B) pred -A. |
| 3004 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 3005 | P = Pred; |
| 3006 | } |
| 3007 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 3008 | // Cases correspond to "max(A, B) p A". |
| 3009 | switch (P) { |
| 3010 | default: |
| 3011 | break; |
| 3012 | case CmpInst::ICMP_EQ: |
| 3013 | case CmpInst::ICMP_ULE: |
| 3014 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 3015 | // in the max/min; if so, we can just return that. |
| 3016 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 3017 | return V; |
| 3018 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 3019 | return V; |
| 3020 | // Otherwise, see if "A EqP B" simplifies. |
| 3021 | if (MaxRecurse) |
| 3022 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
| 3023 | return V; |
| 3024 | break; |
| 3025 | case CmpInst::ICMP_NE: |
| 3026 | case CmpInst::ICMP_UGT: { |
| 3027 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 3028 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 3029 | // tested in the max/min; if so, we can just return that. |
| 3030 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 3031 | return V; |
| 3032 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 3033 | return V; |
| 3034 | // Otherwise, see if "A InvEqP B" simplifies. |
| 3035 | if (MaxRecurse) |
| 3036 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
| 3037 | return V; |
| 3038 | break; |
| 3039 | } |
| 3040 | case CmpInst::ICMP_UGE: |
| 3041 | // Always true. |
| 3042 | return getTrue(ITy); |
| 3043 | case CmpInst::ICMP_ULT: |
| 3044 | // Always false. |
| 3045 | return getFalse(ITy); |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | // Variants on "max(x,y) >= min(x,z)". |
| 3050 | Value *C, *D; |
| 3051 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 3052 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 3053 | (A == C || A == D || B == C || B == D)) { |
| 3054 | // max(x, ?) pred min(x, ?). |
| 3055 | if (Pred == CmpInst::ICMP_SGE) |
| 3056 | // Always true. |
| 3057 | return getTrue(ITy); |
| 3058 | if (Pred == CmpInst::ICMP_SLT) |
| 3059 | // Always false. |
| 3060 | return getFalse(ITy); |
| 3061 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 3062 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 3063 | (A == C || A == D || B == C || B == D)) { |
| 3064 | // min(x, ?) pred max(x, ?). |
| 3065 | if (Pred == CmpInst::ICMP_SLE) |
| 3066 | // Always true. |
| 3067 | return getTrue(ITy); |
| 3068 | if (Pred == CmpInst::ICMP_SGT) |
| 3069 | // Always false. |
| 3070 | return getFalse(ITy); |
| 3071 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 3072 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 3073 | (A == C || A == D || B == C || B == D)) { |
| 3074 | // max(x, ?) pred min(x, ?). |
| 3075 | if (Pred == CmpInst::ICMP_UGE) |
| 3076 | // Always true. |
| 3077 | return getTrue(ITy); |
| 3078 | if (Pred == CmpInst::ICMP_ULT) |
| 3079 | // Always false. |
| 3080 | return getFalse(ITy); |
| 3081 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 3082 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 3083 | (A == C || A == D || B == C || B == D)) { |
| 3084 | // min(x, ?) pred max(x, ?). |
| 3085 | if (Pred == CmpInst::ICMP_ULE) |
| 3086 | // Always true. |
| 3087 | return getTrue(ITy); |
| 3088 | if (Pred == CmpInst::ICMP_UGT) |
| 3089 | // Always false. |
| 3090 | return getFalse(ITy); |
| 3091 | } |
| 3092 | |
| 3093 | return nullptr; |
| 3094 | } |
| 3095 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3096 | /// Given operands for an ICmpInst, see if we can fold the result. |
| 3097 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3098 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3099 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 3100 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3101 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3102 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3103 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | cdfb80d | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 3104 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3105 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3106 | |
| 3107 | // If we have a constant, make sure it is on the RHS. |
| 3108 | std::swap(LHS, RHS); |
| 3109 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3110 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3112 | Type *ITy = GetCompareTy(LHS); // The return type. |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3113 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3114 | // icmp X, X -> true/false |
Chris Lattner | 3afc072 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 3115 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 3116 | // because X could be 0. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3117 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3118 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3119 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 3120 | if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q)) |
| 3121 | return V; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 3122 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 3123 | if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q)) |
| 3124 | return V; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 3125 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 3126 | if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS)) |
| 3127 | return V; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 3128 | |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3129 | // If both operands have range metadata, use the metadata |
| 3130 | // to simplify the comparison. |
| 3131 | if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) { |
Craig Topper | 0c19861 | 2017-04-10 19:37:10 +0000 | [diff] [blame] | 3132 | auto RHS_Instr = cast<Instruction>(RHS); |
| 3133 | auto LHS_Instr = cast<Instruction>(LHS); |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3134 | |
| 3135 | if (RHS_Instr->getMetadata(LLVMContext::MD_range) && |
| 3136 | LHS_Instr->getMetadata(LLVMContext::MD_range)) { |
Sanjoy Das | a7e1378 | 2015-10-24 05:37:35 +0000 | [diff] [blame] | 3137 | auto RHS_CR = getConstantRangeFromMetadata( |
| 3138 | *RHS_Instr->getMetadata(LLVMContext::MD_range)); |
| 3139 | auto LHS_CR = getConstantRangeFromMetadata( |
| 3140 | *LHS_Instr->getMetadata(LLVMContext::MD_range)); |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3141 | |
| 3142 | auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR); |
| 3143 | if (Satisfied_CR.contains(LHS_CR)) |
| 3144 | return ConstantInt::getTrue(RHS->getContext()); |
| 3145 | |
| 3146 | auto InversedSatisfied_CR = ConstantRange::makeSatisfyingICmpRegion( |
| 3147 | CmpInst::getInversePredicate(Pred), RHS_CR); |
| 3148 | if (InversedSatisfied_CR.contains(LHS_CR)) |
| 3149 | return ConstantInt::getFalse(RHS->getContext()); |
| 3150 | } |
| 3151 | } |
| 3152 | |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3153 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 3154 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 3155 | Instruction *LI = cast<CastInst>(LHS); |
| 3156 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3157 | Type *SrcTy = SrcOp->getType(); |
| 3158 | Type *DstTy = LI->getType(); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3159 | |
| 3160 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 3161 | // if the integer type is the same size as the pointer type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3162 | if (MaxRecurse && isa<PtrToIntInst>(LI) && |
| 3163 | Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) { |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3164 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 3165 | // Transfer the cast to the constant. |
| 3166 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 3167 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3168 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3169 | return V; |
| 3170 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 3171 | if (RI->getOperand(0)->getType() == SrcTy) |
| 3172 | // Compare without the cast. |
| 3173 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3174 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3175 | return V; |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | if (isa<ZExtInst>(LHS)) { |
| 3180 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 3181 | // same type. |
| 3182 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 3183 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 3184 | // Compare X and Y. Note that signed predicates become unsigned. |
| 3185 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3186 | SrcOp, RI->getOperand(0), Q, |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3187 | MaxRecurse-1)) |
| 3188 | return V; |
| 3189 | } |
| 3190 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 3191 | // too. If not, then try to deduce the result of the comparison. |
| 3192 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 3193 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3194 | // reextended to DstTy. |
| 3195 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 3196 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 3197 | |
| 3198 | // If the re-extended constant didn't change then this is effectively |
| 3199 | // also a case of comparing two zero-extended values. |
| 3200 | if (RExt == CI && MaxRecurse) |
| 3201 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3202 | SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3203 | return V; |
| 3204 | |
| 3205 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 3206 | // there. Use this to work out the result of the comparison. |
| 3207 | if (RExt != CI) { |
| 3208 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3209 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3210 | // LHS <u RHS. |
| 3211 | case ICmpInst::ICMP_EQ: |
| 3212 | case ICmpInst::ICMP_UGT: |
| 3213 | case ICmpInst::ICMP_UGE: |
| 3214 | return ConstantInt::getFalse(CI->getContext()); |
| 3215 | |
| 3216 | case ICmpInst::ICMP_NE: |
| 3217 | case ICmpInst::ICMP_ULT: |
| 3218 | case ICmpInst::ICMP_ULE: |
| 3219 | return ConstantInt::getTrue(CI->getContext()); |
| 3220 | |
| 3221 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 3222 | // is non-negative then LHS <s RHS. |
| 3223 | case ICmpInst::ICMP_SGT: |
| 3224 | case ICmpInst::ICMP_SGE: |
| 3225 | return CI->getValue().isNegative() ? |
| 3226 | ConstantInt::getTrue(CI->getContext()) : |
| 3227 | ConstantInt::getFalse(CI->getContext()); |
| 3228 | |
| 3229 | case ICmpInst::ICMP_SLT: |
| 3230 | case ICmpInst::ICMP_SLE: |
| 3231 | return CI->getValue().isNegative() ? |
| 3232 | ConstantInt::getFalse(CI->getContext()) : |
| 3233 | ConstantInt::getTrue(CI->getContext()); |
| 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | if (isa<SExtInst>(LHS)) { |
| 3240 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 3241 | // same type. |
| 3242 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 3243 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 3244 | // Compare X and Y. Note that the predicate does not change. |
| 3245 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3246 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3247 | return V; |
| 3248 | } |
| 3249 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 3250 | // too. If not, then try to deduce the result of the comparison. |
| 3251 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 3252 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3253 | // reextended to DstTy. |
| 3254 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 3255 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 3256 | |
| 3257 | // If the re-extended constant didn't change then this is effectively |
| 3258 | // also a case of comparing two sign-extended values. |
| 3259 | if (RExt == CI && MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3260 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3261 | return V; |
| 3262 | |
| 3263 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 3264 | // bits there. Use this to work out the result of the comparison. |
| 3265 | if (RExt != CI) { |
| 3266 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3267 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3268 | case ICmpInst::ICMP_EQ: |
| 3269 | return ConstantInt::getFalse(CI->getContext()); |
| 3270 | case ICmpInst::ICMP_NE: |
| 3271 | return ConstantInt::getTrue(CI->getContext()); |
| 3272 | |
| 3273 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 3274 | // LHS >s RHS. |
| 3275 | case ICmpInst::ICMP_SGT: |
| 3276 | case ICmpInst::ICMP_SGE: |
| 3277 | return CI->getValue().isNegative() ? |
| 3278 | ConstantInt::getTrue(CI->getContext()) : |
| 3279 | ConstantInt::getFalse(CI->getContext()); |
| 3280 | case ICmpInst::ICMP_SLT: |
| 3281 | case ICmpInst::ICMP_SLE: |
| 3282 | return CI->getValue().isNegative() ? |
| 3283 | ConstantInt::getFalse(CI->getContext()) : |
| 3284 | ConstantInt::getTrue(CI->getContext()); |
| 3285 | |
| 3286 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 3287 | // LHS >u RHS. |
| 3288 | case ICmpInst::ICMP_UGT: |
| 3289 | case ICmpInst::ICMP_UGE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3290 | // Comparison is true iff the LHS <s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3291 | if (MaxRecurse) |
| 3292 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 3293 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3294 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3295 | return V; |
| 3296 | break; |
| 3297 | case ICmpInst::ICMP_ULT: |
| 3298 | case ICmpInst::ICMP_ULE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3299 | // Comparison is true iff the LHS >=s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3300 | if (MaxRecurse) |
| 3301 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 3302 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3303 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3304 | return V; |
| 3305 | break; |
| 3306 | } |
| 3307 | } |
| 3308 | } |
| 3309 | } |
| 3310 | } |
| 3311 | |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 3312 | // icmp eq|ne X, Y -> false|true if X != Y |
| 3313 | if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3314 | isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT)) { |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 3315 | LLVMContext &Ctx = LHS->getType()->getContext(); |
| 3316 | return Pred == ICmpInst::ICMP_NE ? |
| 3317 | ConstantInt::getTrue(Ctx) : ConstantInt::getFalse(Ctx); |
| 3318 | } |
Junmo Park | 53470fc | 2016-04-05 21:14:31 +0000 | [diff] [blame] | 3319 | |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 3320 | if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse)) |
| 3321 | return V; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 3322 | |
Sanjay Patel | 35289c6 | 2016-12-10 17:40:47 +0000 | [diff] [blame] | 3323 | if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse)) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 3324 | return V; |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3325 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3326 | // Simplify comparisons of related pointers using a powerful, recursive |
| 3327 | // GEP-walk when we have target data available.. |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 3328 | if (LHS->getType()->isPointerTy()) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 3329 | if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI, LHS, RHS)) |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3330 | return C; |
David Majnemer | dc8767a | 2016-08-07 07:58:10 +0000 | [diff] [blame] | 3331 | if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS)) |
| 3332 | if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS)) |
| 3333 | if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) == |
| 3334 | Q.DL.getTypeSizeInBits(CLHS->getType()) && |
| 3335 | Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) == |
| 3336 | Q.DL.getTypeSizeInBits(CRHS->getType())) |
| 3337 | if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI, |
| 3338 | CLHS->getPointerOperand(), |
| 3339 | CRHS->getPointerOperand())) |
| 3340 | return C; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3341 | |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3342 | if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { |
| 3343 | if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { |
| 3344 | if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && |
| 3345 | GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && |
| 3346 | (ICmpInst::isEquality(Pred) || |
| 3347 | (GLHS->isInBounds() && GRHS->isInBounds() && |
| 3348 | Pred == ICmpInst::getSignedPredicate(Pred)))) { |
| 3349 | // The bases are equal and the indices are constant. Build a constant |
| 3350 | // expression GEP with the same indices and a null base pointer to see |
| 3351 | // what constant folding can make out of it. |
| 3352 | Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); |
| 3353 | SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3354 | Constant *NewLHS = ConstantExpr::getGetElementPtr( |
| 3355 | GLHS->getSourceElementType(), Null, IndicesLHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3356 | |
| 3357 | SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3358 | Constant *NewRHS = ConstantExpr::getGetElementPtr( |
| 3359 | GLHS->getSourceElementType(), Null, IndicesRHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3360 | return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); |
| 3361 | } |
| 3362 | } |
| 3363 | } |
| 3364 | |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3365 | // If a bit is known to be zero for A and known to be one for B, |
| 3366 | // then A and B cannot be equal. |
| 3367 | if (ICmpInst::isEquality(Pred)) { |
Sanjay Patel | bcaf6f3 | 2016-08-04 17:48:04 +0000 | [diff] [blame] | 3368 | const APInt *RHSVal; |
| 3369 | if (match(RHS, m_APInt(RHSVal))) { |
| 3370 | unsigned BitWidth = RHSVal->getBitWidth(); |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3371 | APInt LHSKnownZero(BitWidth, 0); |
| 3372 | APInt LHSKnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3373 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, Q.DL, /*Depth=*/0, Q.AC, |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3374 | Q.CxtI, Q.DT); |
Sanjay Patel | bcaf6f3 | 2016-08-04 17:48:04 +0000 | [diff] [blame] | 3375 | if (((LHSKnownZero & *RHSVal) != 0) || ((LHSKnownOne & ~(*RHSVal)) != 0)) |
| 3376 | return Pred == ICmpInst::ICMP_EQ ? ConstantInt::getFalse(ITy) |
| 3377 | : ConstantInt::getTrue(ITy); |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3378 | } |
| 3379 | } |
| 3380 | |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3381 | // If the comparison is with the result of a select instruction, check whether |
| 3382 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3383 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3384 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3385 | return V; |
| 3386 | |
| 3387 | // If the comparison is with the result of a phi instruction, check whether |
| 3388 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3389 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3390 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3391 | return V; |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3392 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3393 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3396 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3397 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3398 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3399 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 85dbea9 | 2015-12-24 09:08:08 +0000 | [diff] [blame] | 3400 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3401 | return ::SimplifyICmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3402 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3405 | /// Given operands for an FCmpInst, see if we can fold the result. |
| 3406 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3407 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3408 | FastMathFlags FMF, const Query &Q, |
| 3409 | unsigned MaxRecurse) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3410 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 3411 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 3412 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3413 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3414 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3415 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3416 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3417 | // If we have a constant, make sure it is on the RHS. |
| 3418 | std::swap(LHS, RHS); |
| 3419 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3420 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3421 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3422 | // Fold trivial predicates. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3423 | Type *RetTy = GetCompareTy(LHS); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3424 | if (Pred == FCmpInst::FCMP_FALSE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3425 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3426 | if (Pred == FCmpInst::FCMP_TRUE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3427 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3428 | |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3429 | // UNO/ORD predicates can be trivially folded if NaNs are ignored. |
| 3430 | if (FMF.noNaNs()) { |
| 3431 | if (Pred == FCmpInst::FCMP_UNO) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3432 | return getFalse(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3433 | if (Pred == FCmpInst::FCMP_ORD) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3434 | return getTrue(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3437 | // fcmp pred x, undef and fcmp pred undef, x |
| 3438 | // fold to true if unordered, false if ordered |
| 3439 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) { |
| 3440 | // Choosing NaN for the undef will always make unordered comparison succeed |
| 3441 | // and ordered comparison fail. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3442 | return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred)); |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3443 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3444 | |
| 3445 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3446 | if (LHS == RHS) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3447 | if (CmpInst::isTrueWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3448 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3449 | if (CmpInst::isFalseWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3450 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3451 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3452 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3453 | // Handle fcmp with constant RHS |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3454 | const ConstantFP *CFP = nullptr; |
| 3455 | if (const auto *RHSC = dyn_cast<Constant>(RHS)) { |
| 3456 | if (RHS->getType()->isVectorTy()) |
| 3457 | CFP = dyn_cast_or_null<ConstantFP>(RHSC->getSplatValue()); |
| 3458 | else |
| 3459 | CFP = dyn_cast<ConstantFP>(RHSC); |
| 3460 | } |
| 3461 | if (CFP) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3462 | // If the constant is a nan, see if we can fold the comparison based on it. |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3463 | if (CFP->getValueAPF().isNaN()) { |
| 3464 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3465 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3466 | assert(FCmpInst::isUnordered(Pred) && |
| 3467 | "Comparison must be either ordered or unordered!"); |
| 3468 | // True if unordered. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3469 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3470 | } |
| 3471 | // Check whether the constant is an infinity. |
| 3472 | if (CFP->getValueAPF().isInfinity()) { |
| 3473 | if (CFP->getValueAPF().isNegative()) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3474 | switch (Pred) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3475 | case FCmpInst::FCMP_OLT: |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3476 | // No value is ordered and less than negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3477 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3478 | case FCmpInst::FCMP_UGE: |
| 3479 | // All values are unordered with or at least negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3480 | return getTrue(RetTy); |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3481 | default: |
| 3482 | break; |
| 3483 | } |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3484 | } else { |
| 3485 | switch (Pred) { |
| 3486 | case FCmpInst::FCMP_OGT: |
| 3487 | // No value is ordered and greater than infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3488 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3489 | case FCmpInst::FCMP_ULE: |
| 3490 | // All values are unordered with and at most infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3491 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3492 | default: |
| 3493 | break; |
| 3494 | } |
| 3495 | } |
| 3496 | } |
| 3497 | if (CFP->getValueAPF().isZero()) { |
| 3498 | switch (Pred) { |
| 3499 | case FCmpInst::FCMP_UGE: |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3500 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3501 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3502 | break; |
| 3503 | case FCmpInst::FCMP_OLT: |
| 3504 | // X < 0 |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3505 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3506 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3507 | break; |
| 3508 | default: |
| 3509 | break; |
| 3510 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3511 | } |
| 3512 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3513 | |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3514 | // If the comparison is with the result of a select instruction, check whether |
| 3515 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3516 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3517 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3518 | return V; |
| 3519 | |
| 3520 | // If the comparison is with the result of a phi instruction, check whether |
| 3521 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3522 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3523 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3524 | return V; |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3525 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3526 | return nullptr; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3529 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3530 | FastMathFlags FMF, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3531 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3532 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3533 | const Instruction *CxtI) { |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3534 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, FMF, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3535 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3538 | /// See if V simplifies when its operand Op is replaced with RepOp. |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3539 | static const Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
| 3540 | const Query &Q, |
| 3541 | unsigned MaxRecurse) { |
| 3542 | // Trivial replacement. |
| 3543 | if (V == Op) |
| 3544 | return RepOp; |
| 3545 | |
| 3546 | auto *I = dyn_cast<Instruction>(V); |
| 3547 | if (!I) |
| 3548 | return nullptr; |
| 3549 | |
| 3550 | // If this is a binary operator, try to simplify it with the replaced op. |
| 3551 | if (auto *B = dyn_cast<BinaryOperator>(I)) { |
| 3552 | // Consider: |
| 3553 | // %cmp = icmp eq i32 %x, 2147483647 |
| 3554 | // %add = add nsw i32 %x, 1 |
| 3555 | // %sel = select i1 %cmp, i32 -2147483648, i32 %add |
| 3556 | // |
| 3557 | // We can't replace %sel with %add unless we strip away the flags. |
| 3558 | if (isa<OverflowingBinaryOperator>(B)) |
| 3559 | if (B->hasNoSignedWrap() || B->hasNoUnsignedWrap()) |
| 3560 | return nullptr; |
| 3561 | if (isa<PossiblyExactOperator>(B)) |
| 3562 | if (B->isExact()) |
| 3563 | return nullptr; |
| 3564 | |
| 3565 | if (MaxRecurse) { |
| 3566 | if (B->getOperand(0) == Op) |
| 3567 | return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), Q, |
| 3568 | MaxRecurse - 1); |
| 3569 | if (B->getOperand(1) == Op) |
| 3570 | return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, Q, |
| 3571 | MaxRecurse - 1); |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | // Same for CmpInsts. |
| 3576 | if (CmpInst *C = dyn_cast<CmpInst>(I)) { |
| 3577 | if (MaxRecurse) { |
| 3578 | if (C->getOperand(0) == Op) |
| 3579 | return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), Q, |
| 3580 | MaxRecurse - 1); |
| 3581 | if (C->getOperand(1) == Op) |
| 3582 | return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, Q, |
| 3583 | MaxRecurse - 1); |
| 3584 | } |
| 3585 | } |
| 3586 | |
| 3587 | // TODO: We could hand off more cases to instsimplify here. |
| 3588 | |
| 3589 | // If all operands are constant after substituting Op for RepOp then we can |
| 3590 | // constant fold the instruction. |
| 3591 | if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) { |
| 3592 | // Build a list of all constant operands. |
| 3593 | SmallVector<Constant *, 8> ConstOps; |
| 3594 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3595 | if (I->getOperand(i) == Op) |
| 3596 | ConstOps.push_back(CRepOp); |
| 3597 | else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i))) |
| 3598 | ConstOps.push_back(COp); |
| 3599 | else |
| 3600 | break; |
| 3601 | } |
| 3602 | |
| 3603 | // All operands were constants, fold it. |
| 3604 | if (ConstOps.size() == I->getNumOperands()) { |
| 3605 | if (CmpInst *C = dyn_cast<CmpInst>(I)) |
| 3606 | return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0], |
| 3607 | ConstOps[1], Q.DL, Q.TLI); |
| 3608 | |
| 3609 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 3610 | if (!LI->isVolatile()) |
Eduard Burtescu | 1423921 | 2016-01-22 01:17:26 +0000 | [diff] [blame] | 3611 | return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3612 | |
Manuel Jacob | e902459 | 2016-01-21 06:33:22 +0000 | [diff] [blame] | 3613 | return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | return nullptr; |
| 3618 | } |
| 3619 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3620 | /// Try to simplify a select instruction when its condition operand is an |
| 3621 | /// integer comparison where one operand of the compare is a constant. |
| 3622 | static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X, |
| 3623 | const APInt *Y, bool TrueWhenUnset) { |
| 3624 | const APInt *C; |
| 3625 | |
| 3626 | // (X & Y) == 0 ? X & ~Y : X --> X |
| 3627 | // (X & Y) != 0 ? X & ~Y : X --> X & ~Y |
| 3628 | if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3629 | *Y == ~*C) |
| 3630 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3631 | |
| 3632 | // (X & Y) == 0 ? X : X & ~Y --> X & ~Y |
| 3633 | // (X & Y) != 0 ? X : X & ~Y --> X |
| 3634 | if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3635 | *Y == ~*C) |
| 3636 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3637 | |
| 3638 | if (Y->isPowerOf2()) { |
| 3639 | // (X & Y) == 0 ? X | Y : X --> X | Y |
| 3640 | // (X & Y) != 0 ? X | Y : X --> X |
| 3641 | if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3642 | *Y == *C) |
| 3643 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3644 | |
| 3645 | // (X & Y) == 0 ? X : X | Y --> X |
| 3646 | // (X & Y) != 0 ? X : X | Y --> X | Y |
| 3647 | if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3648 | *Y == *C) |
| 3649 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3650 | } |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 3651 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3652 | return nullptr; |
| 3653 | } |
| 3654 | |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3655 | /// An alternative way to test if a bit is set or not uses sgt/slt instead of |
| 3656 | /// eq/ne. |
| 3657 | static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *TrueVal, |
| 3658 | Value *FalseVal, |
| 3659 | bool TrueWhenUnset) { |
| 3660 | unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits(); |
Sanjay Patel | e9fc79b | 2016-07-21 21:56:00 +0000 | [diff] [blame] | 3661 | if (!BitWidth) |
| 3662 | return nullptr; |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 3663 | |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3664 | APInt MinSignedValue; |
| 3665 | Value *X; |
| 3666 | if (match(CmpLHS, m_Trunc(m_Value(X))) && (X == TrueVal || X == FalseVal)) { |
| 3667 | // icmp slt (trunc X), 0 <--> icmp ne (and X, C), 0 |
| 3668 | // icmp sgt (trunc X), -1 <--> icmp eq (and X, C), 0 |
| 3669 | unsigned DestSize = CmpLHS->getType()->getScalarSizeInBits(); |
| 3670 | MinSignedValue = APInt::getSignedMinValue(DestSize).zext(BitWidth); |
| 3671 | } else { |
| 3672 | // icmp slt X, 0 <--> icmp ne (and X, C), 0 |
| 3673 | // icmp sgt X, -1 <--> icmp eq (and X, C), 0 |
| 3674 | X = CmpLHS; |
| 3675 | MinSignedValue = APInt::getSignedMinValue(BitWidth); |
| 3676 | } |
| 3677 | |
| 3678 | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, &MinSignedValue, |
| 3679 | TrueWhenUnset)) |
| 3680 | return V; |
| 3681 | |
| 3682 | return nullptr; |
| 3683 | } |
| 3684 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3685 | /// Try to simplify a select instruction when its condition operand is an |
| 3686 | /// integer comparison. |
| 3687 | static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, |
| 3688 | Value *FalseVal, const Query &Q, |
| 3689 | unsigned MaxRecurse) { |
| 3690 | ICmpInst::Predicate Pred; |
| 3691 | Value *CmpLHS, *CmpRHS; |
| 3692 | if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) |
| 3693 | return nullptr; |
| 3694 | |
Sanjay Patel | 5f3c703 | 2016-07-20 23:40:01 +0000 | [diff] [blame] | 3695 | // FIXME: This code is nearly duplicated in InstCombine. Using/refactoring |
| 3696 | // decomposeBitTestICmp() might help. |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3697 | if (ICmpInst::isEquality(Pred) && match(CmpRHS, m_Zero())) { |
| 3698 | Value *X; |
| 3699 | const APInt *Y; |
| 3700 | if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y)))) |
| 3701 | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y, |
| 3702 | Pred == ICmpInst::ICMP_EQ)) |
| 3703 | return V; |
| 3704 | } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) { |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3705 | // Comparing signed-less-than 0 checks if the sign bit is set. |
| 3706 | if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal, |
| 3707 | false)) |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3708 | return V; |
| 3709 | } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) { |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3710 | // Comparing signed-greater-than -1 checks if the sign bit is not set. |
| 3711 | if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal, |
| 3712 | true)) |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3713 | return V; |
| 3714 | } |
| 3715 | |
| 3716 | if (CondVal->hasOneUse()) { |
| 3717 | const APInt *C; |
| 3718 | if (match(CmpRHS, m_APInt(C))) { |
| 3719 | // X < MIN ? T : F --> F |
| 3720 | if (Pred == ICmpInst::ICMP_SLT && C->isMinSignedValue()) |
| 3721 | return FalseVal; |
| 3722 | // X < MIN ? T : F --> F |
| 3723 | if (Pred == ICmpInst::ICMP_ULT && C->isMinValue()) |
| 3724 | return FalseVal; |
| 3725 | // X > MAX ? T : F --> F |
| 3726 | if (Pred == ICmpInst::ICMP_SGT && C->isMaxSignedValue()) |
| 3727 | return FalseVal; |
| 3728 | // X > MAX ? T : F --> F |
| 3729 | if (Pred == ICmpInst::ICMP_UGT && C->isMaxValue()) |
| 3730 | return FalseVal; |
| 3731 | } |
| 3732 | } |
| 3733 | |
| 3734 | // If we have an equality comparison, then we know the value in one of the |
| 3735 | // arms of the select. See if substituting this value into the arm and |
| 3736 | // simplifying the result yields the same value as the other arm. |
| 3737 | if (Pred == ICmpInst::ICMP_EQ) { |
| 3738 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3739 | TrueVal || |
| 3740 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3741 | TrueVal) |
| 3742 | return FalseVal; |
| 3743 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3744 | FalseVal || |
| 3745 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3746 | FalseVal) |
| 3747 | return FalseVal; |
| 3748 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 3749 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3750 | FalseVal || |
| 3751 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3752 | FalseVal) |
| 3753 | return TrueVal; |
| 3754 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3755 | TrueVal || |
| 3756 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3757 | TrueVal) |
| 3758 | return TrueVal; |
| 3759 | } |
| 3760 | |
| 3761 | return nullptr; |
| 3762 | } |
| 3763 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3764 | /// Given operands for a SelectInst, see if we can fold the result. |
| 3765 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3766 | static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal, |
| 3767 | Value *FalseVal, const Query &Q, |
| 3768 | unsigned MaxRecurse) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3769 | // select true, X, Y -> X |
| 3770 | // select false, X, Y -> Y |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3771 | if (Constant *CB = dyn_cast<Constant>(CondVal)) { |
| 3772 | if (CB->isAllOnesValue()) |
| 3773 | return TrueVal; |
| 3774 | if (CB->isNullValue()) |
| 3775 | return FalseVal; |
| 3776 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3777 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3778 | // select C, X, X -> X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3779 | if (TrueVal == FalseVal) |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3780 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3781 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3782 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 3783 | if (isa<Constant>(TrueVal)) |
| 3784 | return TrueVal; |
| 3785 | return FalseVal; |
| 3786 | } |
Dan Gohman | 54664ed | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 3787 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 3788 | return FalseVal; |
| 3789 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 3790 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3791 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3792 | if (Value *V = |
| 3793 | simplifySelectWithICmpCond(CondVal, TrueVal, FalseVal, Q, MaxRecurse)) |
| 3794 | return V; |
David Majnemer | c6a5e1d | 2014-11-27 06:32:46 +0000 | [diff] [blame] | 3795 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3796 | return nullptr; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3799 | Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3800 | const DataLayout &DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3801 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3802 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3803 | const Instruction *CxtI) { |
| 3804 | return ::SimplifySelectInst(Cond, TrueVal, FalseVal, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3805 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3808 | /// Given operands for an GetElementPtrInst, see if we can fold the result. |
| 3809 | /// If not, this returns null. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3810 | static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
| 3811 | const Query &Q, unsigned) { |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3812 | // The type of the GEP pointer operand. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3813 | unsigned AS = |
| 3814 | cast<PointerType>(Ops[0]->getType()->getScalarType())->getAddressSpace(); |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3815 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3816 | // getelementptr P -> P. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3817 | if (Ops.size() == 1) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3818 | return Ops[0]; |
| 3819 | |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3820 | // Compute the (pointer) type returned by the GEP instruction. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3821 | Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Ops.slice(1)); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3822 | Type *GEPTy = PointerType::get(LastType, AS); |
| 3823 | if (VectorType *VT = dyn_cast<VectorType>(Ops[0]->getType())) |
| 3824 | GEPTy = VectorType::get(GEPTy, VT->getNumElements()); |
Davide Italiano | a9f047a | 2017-04-19 14:23:42 +0000 | [diff] [blame] | 3825 | else if (VectorType *VT = dyn_cast<VectorType>(Ops[1]->getType())) |
| 3826 | GEPTy = VectorType::get(GEPTy, VT->getNumElements()); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3827 | |
| 3828 | if (isa<UndefValue>(Ops[0])) |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3829 | return UndefValue::get(GEPTy); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3830 | |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3831 | if (Ops.size() == 2) { |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3832 | // getelementptr P, 0 -> P. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3833 | if (match(Ops[1], m_Zero())) |
| 3834 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3835 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3836 | Type *Ty = SrcTy; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3837 | if (Ty->isSized()) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3838 | Value *P; |
| 3839 | uint64_t C; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3840 | uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3841 | // getelementptr P, N -> P if P points to a type of zero size. |
| 3842 | if (TyAllocSize == 0) |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3843 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3844 | |
| 3845 | // The following transforms are only safe if the ptrtoint cast |
| 3846 | // doesn't truncate the pointers. |
| 3847 | if (Ops[1]->getType()->getScalarSizeInBits() == |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3848 | Q.DL.getPointerSizeInBits(AS)) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3849 | auto PtrToIntOrZero = [GEPTy](Value *P) -> Value * { |
| 3850 | if (match(P, m_Zero())) |
| 3851 | return Constant::getNullValue(GEPTy); |
| 3852 | Value *Temp; |
| 3853 | if (match(P, m_PtrToInt(m_Value(Temp)))) |
David Majnemer | 11ca297 | 2014-08-27 20:08:34 +0000 | [diff] [blame] | 3854 | if (Temp->getType() == GEPTy) |
| 3855 | return Temp; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3856 | return nullptr; |
| 3857 | }; |
| 3858 | |
| 3859 | // getelementptr V, (sub P, V) -> P if P points to a type of size 1. |
| 3860 | if (TyAllocSize == 1 && |
| 3861 | match(Ops[1], m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))))) |
| 3862 | if (Value *R = PtrToIntOrZero(P)) |
| 3863 | return R; |
| 3864 | |
| 3865 | // getelementptr V, (ashr (sub P, V), C) -> Q |
| 3866 | // if P points to a type of size 1 << C. |
| 3867 | if (match(Ops[1], |
| 3868 | m_AShr(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3869 | m_ConstantInt(C))) && |
| 3870 | TyAllocSize == 1ULL << C) |
| 3871 | if (Value *R = PtrToIntOrZero(P)) |
| 3872 | return R; |
| 3873 | |
| 3874 | // getelementptr V, (sdiv (sub P, V), C) -> Q |
| 3875 | // if P points to a type of size C. |
| 3876 | if (match(Ops[1], |
| 3877 | m_SDiv(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3878 | m_SpecificInt(TyAllocSize)))) |
| 3879 | if (Value *R = PtrToIntOrZero(P)) |
| 3880 | return R; |
| 3881 | } |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3882 | } |
| 3883 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3884 | |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3885 | if (Q.DL.getTypeAllocSize(LastType) == 1 && |
| 3886 | all_of(Ops.slice(1).drop_back(1), |
| 3887 | [](Value *Idx) { return match(Idx, m_Zero()); })) { |
| 3888 | unsigned PtrWidth = |
| 3889 | Q.DL.getPointerSizeInBits(Ops[0]->getType()->getPointerAddressSpace()); |
| 3890 | if (Q.DL.getTypeSizeInBits(Ops.back()->getType()) == PtrWidth) { |
| 3891 | APInt BasePtrOffset(PtrWidth, 0); |
| 3892 | Value *StrippedBasePtr = |
| 3893 | Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.DL, |
| 3894 | BasePtrOffset); |
| 3895 | |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3896 | // gep (gep V, C), (sub 0, V) -> C |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3897 | if (match(Ops.back(), |
| 3898 | m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr))))) { |
| 3899 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset); |
| 3900 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3901 | } |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3902 | // gep (gep V, C), (xor V, -1) -> C-1 |
| 3903 | if (match(Ops.back(), |
| 3904 | m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes()))) { |
| 3905 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1); |
| 3906 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3907 | } |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3908 | } |
| 3909 | } |
| 3910 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3911 | // Check to see if this is constant foldable. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3912 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3913 | if (!isa<Constant>(Ops[i])) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3914 | return nullptr; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3915 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3916 | return ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ops[0]), |
| 3917 | Ops.slice(1)); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 3920 | Value *llvm::SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
| 3921 | const DataLayout &DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3922 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3923 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3924 | const Instruction *CxtI) { |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 3925 | return ::SimplifyGEPInst(SrcTy, Ops, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3926 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3929 | /// Given operands for an InsertValueInst, see if we can fold the result. |
| 3930 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3931 | static Value *SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 3932 | ArrayRef<unsigned> Idxs, const Query &Q, |
| 3933 | unsigned) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3934 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 3935 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 3936 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 3937 | |
| 3938 | // insertvalue x, undef, n -> x |
| 3939 | if (match(Val, m_Undef())) |
| 3940 | return Agg; |
| 3941 | |
| 3942 | // insertvalue x, (extractvalue y, n), n |
| 3943 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | 4b79c21 | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 3944 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 3945 | EV->getIndices() == Idxs) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3946 | // insertvalue undef, (extractvalue y, n), n -> y |
| 3947 | if (match(Agg, m_Undef())) |
| 3948 | return EV->getAggregateOperand(); |
| 3949 | |
| 3950 | // insertvalue y, (extractvalue y, n), n -> y |
| 3951 | if (Agg == EV->getAggregateOperand()) |
| 3952 | return Agg; |
| 3953 | } |
| 3954 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3955 | return nullptr; |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3958 | Value *llvm::SimplifyInsertValueInst( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3959 | Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, const DataLayout &DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3960 | const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3961 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3962 | return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3963 | RecursionLimit); |
| 3964 | } |
| 3965 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3966 | /// Given operands for an ExtractValueInst, see if we can fold the result. |
| 3967 | /// If not, this returns null. |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3968 | static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
| 3969 | const Query &, unsigned) { |
| 3970 | if (auto *CAgg = dyn_cast<Constant>(Agg)) |
| 3971 | return ConstantFoldExtractValueInstruction(CAgg, Idxs); |
| 3972 | |
| 3973 | // extractvalue x, (insertvalue y, elt, n), n -> elt |
| 3974 | unsigned NumIdxs = Idxs.size(); |
| 3975 | for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr; |
| 3976 | IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) { |
| 3977 | ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices(); |
| 3978 | unsigned NumInsertValueIdxs = InsertValueIdxs.size(); |
| 3979 | unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs); |
| 3980 | if (InsertValueIdxs.slice(0, NumCommonIdxs) == |
| 3981 | Idxs.slice(0, NumCommonIdxs)) { |
| 3982 | if (NumIdxs == NumInsertValueIdxs) |
| 3983 | return IVI->getInsertedValueOperand(); |
| 3984 | break; |
| 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | return nullptr; |
| 3989 | } |
| 3990 | |
| 3991 | Value *llvm::SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
| 3992 | const DataLayout &DL, |
| 3993 | const TargetLibraryInfo *TLI, |
| 3994 | const DominatorTree *DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3995 | AssumptionCache *AC, |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3996 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3997 | return ::SimplifyExtractValueInst(Agg, Idxs, Query(DL, TLI, DT, AC, CxtI), |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3998 | RecursionLimit); |
| 3999 | } |
| 4000 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4001 | /// Given operands for an ExtractElementInst, see if we can fold the result. |
| 4002 | /// If not, this returns null. |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4003 | static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx, const Query &, |
| 4004 | unsigned) { |
| 4005 | if (auto *CVec = dyn_cast<Constant>(Vec)) { |
| 4006 | if (auto *CIdx = dyn_cast<Constant>(Idx)) |
| 4007 | return ConstantFoldExtractElementInstruction(CVec, CIdx); |
| 4008 | |
| 4009 | // The index is not relevant if our vector is a splat. |
| 4010 | if (auto *Splat = CVec->getSplatValue()) |
| 4011 | return Splat; |
| 4012 | |
| 4013 | if (isa<UndefValue>(Vec)) |
| 4014 | return UndefValue::get(Vec->getType()->getVectorElementType()); |
| 4015 | } |
| 4016 | |
| 4017 | // If extracting a specified index from the vector, see if we can recursively |
| 4018 | // find a previously computed scalar that was inserted into the vector. |
David Majnemer | 8e335ca | 2015-08-18 22:18:22 +0000 | [diff] [blame] | 4019 | if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) |
| 4020 | if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue())) |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4021 | return Elt; |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4022 | |
| 4023 | return nullptr; |
| 4024 | } |
| 4025 | |
| 4026 | Value *llvm::SimplifyExtractElementInst( |
| 4027 | Value *Vec, Value *Idx, const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4028 | const DominatorTree *DT, AssumptionCache *AC, const Instruction *CxtI) { |
| 4029 | return ::SimplifyExtractElementInst(Vec, Idx, Query(DL, TLI, DT, AC, CxtI), |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4030 | RecursionLimit); |
| 4031 | } |
| 4032 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4033 | /// See if we can fold the given phi. If not, returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4034 | static Value *SimplifyPHINode(PHINode *PN, const Query &Q) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4035 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 4036 | // with the common value. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4037 | Value *CommonValue = nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4038 | bool HasUndefInput = false; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 4039 | for (Value *Incoming : PN->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4040 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 4041 | if (Incoming == PN) continue; |
| 4042 | if (isa<UndefValue>(Incoming)) { |
| 4043 | // Remember that we saw an undef value, but otherwise ignore them. |
| 4044 | HasUndefInput = true; |
| 4045 | continue; |
| 4046 | } |
| 4047 | if (CommonValue && Incoming != CommonValue) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4048 | return nullptr; // Not the same, bail out. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4049 | CommonValue = Incoming; |
| 4050 | } |
| 4051 | |
| 4052 | // If CommonValue is null then all of the incoming values were either undef or |
| 4053 | // equal to the phi node itself. |
| 4054 | if (!CommonValue) |
| 4055 | return UndefValue::get(PN->getType()); |
| 4056 | |
| 4057 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 4058 | // instruction, we cannot return X as the result of the PHI node unless it |
| 4059 | // dominates the PHI block. |
| 4060 | if (HasUndefInput) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4061 | return ValueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4062 | |
| 4063 | return CommonValue; |
| 4064 | } |
| 4065 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4066 | static Value *SimplifyCastInst(unsigned CastOpc, Value *Op, |
| 4067 | Type *Ty, const Query &Q, unsigned MaxRecurse) { |
David Majnemer | 126de5d | 2016-07-25 03:39:21 +0000 | [diff] [blame] | 4068 | if (auto *C = dyn_cast<Constant>(Op)) |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4069 | return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 4070 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4071 | if (auto *CI = dyn_cast<CastInst>(Op)) { |
| 4072 | auto *Src = CI->getOperand(0); |
| 4073 | Type *SrcTy = Src->getType(); |
| 4074 | Type *MidTy = CI->getType(); |
| 4075 | Type *DstTy = Ty; |
| 4076 | if (Src->getType() == Ty) { |
| 4077 | auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode()); |
| 4078 | auto SecondOp = static_cast<Instruction::CastOps>(CastOpc); |
| 4079 | Type *SrcIntPtrTy = |
| 4080 | SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr; |
| 4081 | Type *MidIntPtrTy = |
| 4082 | MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr; |
| 4083 | Type *DstIntPtrTy = |
| 4084 | DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr; |
| 4085 | if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy, |
| 4086 | SrcIntPtrTy, MidIntPtrTy, |
| 4087 | DstIntPtrTy) == Instruction::BitCast) |
| 4088 | return Src; |
| 4089 | } |
| 4090 | } |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4091 | |
| 4092 | // bitcast x -> x |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4093 | if (CastOpc == Instruction::BitCast) |
| 4094 | if (Op->getType() == Ty) |
| 4095 | return Op; |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4096 | |
| 4097 | return nullptr; |
| 4098 | } |
| 4099 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4100 | Value *llvm::SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, |
| 4101 | const DataLayout &DL, |
| 4102 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4103 | const DominatorTree *DT, AssumptionCache *AC, |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4104 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4105 | return ::SimplifyCastInst(CastOpc, Op, Ty, Query(DL, TLI, DT, AC, CxtI), |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4106 | RecursionLimit); |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4109 | /// For the given destination element of a shuffle, peek through shuffles to |
| 4110 | /// match a root vector source operand that contains that element in the same |
| 4111 | /// vector lane (ie, the same mask index), so we can eliminate the shuffle(s). |
| 4112 | static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1, |
| 4113 | Constant *Mask, Value *RootVec, int RootElt, |
| 4114 | unsigned MaxRecurse) { |
| 4115 | if (!MaxRecurse--) |
| 4116 | return nullptr; |
| 4117 | |
| 4118 | // Bail out if any mask value is undefined. That kind of shuffle may be |
| 4119 | // simplified further based on demanded bits or other folds. |
| 4120 | int MaskVal = ShuffleVectorInst::getMaskValue(Mask, RootElt); |
| 4121 | if (MaskVal == -1) |
| 4122 | return nullptr; |
| 4123 | |
| 4124 | // The mask value chooses which source operand we need to look at next. |
| 4125 | Value *SourceOp; |
| 4126 | int InVecNumElts = Op0->getType()->getVectorNumElements(); |
| 4127 | if (MaskVal < InVecNumElts) { |
| 4128 | RootElt = MaskVal; |
| 4129 | SourceOp = Op0; |
| 4130 | } else { |
| 4131 | RootElt = MaskVal - InVecNumElts; |
| 4132 | SourceOp = Op1; |
| 4133 | } |
| 4134 | |
| 4135 | // If the source operand is a shuffle itself, look through it to find the |
| 4136 | // matching root vector. |
| 4137 | if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) { |
| 4138 | return foldIdentityShuffles( |
| 4139 | DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1), |
| 4140 | SourceShuf->getMask(), RootVec, RootElt, MaxRecurse); |
| 4141 | } |
| 4142 | |
| 4143 | // TODO: Look through bitcasts? What if the bitcast changes the vector element |
| 4144 | // size? |
| 4145 | |
| 4146 | // The source operand is not a shuffle. Initialize the root vector value for |
| 4147 | // this shuffle if that has not been done yet. |
| 4148 | if (!RootVec) |
| 4149 | RootVec = SourceOp; |
| 4150 | |
| 4151 | // Give up as soon as a source operand does not match the existing root value. |
| 4152 | if (RootVec != SourceOp) |
| 4153 | return nullptr; |
| 4154 | |
| 4155 | // The element must be coming from the same lane in the source vector |
| 4156 | // (although it may have crossed lanes in intermediate shuffles). |
| 4157 | if (RootElt != DestElt) |
| 4158 | return nullptr; |
| 4159 | |
| 4160 | return RootVec; |
| 4161 | } |
| 4162 | |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4163 | static Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, |
| 4164 | Type *RetTy, const Query &Q, |
| 4165 | unsigned MaxRecurse) { |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame] | 4166 | Type *InVecTy = Op0->getType(); |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4167 | unsigned MaskNumElts = Mask->getType()->getVectorNumElements(); |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame] | 4168 | unsigned InVecNumElts = InVecTy->getVectorNumElements(); |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4169 | |
| 4170 | auto *Op0Const = dyn_cast<Constant>(Op0); |
| 4171 | auto *Op1Const = dyn_cast<Constant>(Op1); |
| 4172 | |
| 4173 | // If all operands are constant, constant fold the shuffle. |
| 4174 | if (Op0Const && Op1Const) |
| 4175 | return ConstantFoldShuffleVectorInstruction(Op0Const, Op1Const, Mask); |
| 4176 | |
| 4177 | // If only one of the operands is constant, constant fold the shuffle if the |
| 4178 | // mask does not select elements from the variable operand. |
| 4179 | bool MaskSelects0 = false, MaskSelects1 = false; |
| 4180 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
| 4181 | int Idx = ShuffleVectorInst::getMaskValue(Mask, i); |
| 4182 | if (Idx == -1) |
| 4183 | continue; |
| 4184 | if ((unsigned)Idx < InVecNumElts) |
| 4185 | MaskSelects0 = true; |
| 4186 | else |
| 4187 | MaskSelects1 = true; |
| 4188 | } |
| 4189 | if (!MaskSelects0 && Op1Const) |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame] | 4190 | return ConstantFoldShuffleVectorInstruction(UndefValue::get(InVecTy), |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4191 | Op1Const, Mask); |
| 4192 | if (!MaskSelects1 && Op0Const) |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame] | 4193 | return ConstantFoldShuffleVectorInstruction(Op0Const, |
| 4194 | UndefValue::get(InVecTy), Mask); |
| 4195 | |
| 4196 | // A shuffle of a splat is always the splat itself. Legal if the shuffle's |
| 4197 | // value type is same as the input vectors' type. |
| 4198 | if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0)) |
| 4199 | if (!MaskSelects1 && RetTy == InVecTy && |
| 4200 | OpShuf->getMask()->getSplatValue()) |
| 4201 | return Op0; |
| 4202 | if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op1)) |
| 4203 | if (!MaskSelects0 && RetTy == InVecTy && |
| 4204 | OpShuf->getMask()->getSplatValue()) |
| 4205 | return Op1; |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4206 | |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4207 | // Don't fold a shuffle with undef mask elements. This may get folded in a |
| 4208 | // better way using demanded bits or other analysis. |
| 4209 | // TODO: Should we allow this? |
| 4210 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 4211 | if (ShuffleVectorInst::getMaskValue(Mask, i) == -1) |
| 4212 | return nullptr; |
| 4213 | |
| 4214 | // Check if every element of this shuffle can be mapped back to the |
| 4215 | // corresponding element of a single root vector. If so, we don't need this |
| 4216 | // shuffle. This handles simple identity shuffles as well as chains of |
| 4217 | // shuffles that may widen/narrow and/or move elements across lanes and back. |
| 4218 | Value *RootVec = nullptr; |
| 4219 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
| 4220 | // Note that recursion is limited for each vector element, so if any element |
| 4221 | // exceeds the limit, this will fail to simplify. |
| 4222 | RootVec = foldIdentityShuffles(i, Op0, Op1, Mask, RootVec, i, MaxRecurse); |
| 4223 | |
| 4224 | // We can't replace a widening/narrowing shuffle with one of its operands. |
| 4225 | if (!RootVec || RootVec->getType() != RetTy) |
| 4226 | return nullptr; |
| 4227 | } |
| 4228 | return RootVec; |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | /// Given operands for a ShuffleVectorInst, fold the result or return null. |
| 4232 | Value *llvm::SimplifyShuffleVectorInst( |
| 4233 | Value *Op0, Value *Op1, Constant *Mask, Type *RetTy, |
| 4234 | const DataLayout &DL, const TargetLibraryInfo *TLI, const DominatorTree *DT, |
| 4235 | AssumptionCache *AC, const Instruction *CxtI) { |
| 4236 | return ::SimplifyShuffleVectorInst( |
| 4237 | Op0, Op1, Mask, RetTy, Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
| 4238 | } |
| 4239 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4240 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4241 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4242 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 4243 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4244 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4245 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4246 | switch (Opcode) { |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4247 | case Instruction::Add: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4248 | return SimplifyAddInst(LHS, RHS, false, false, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 4249 | case Instruction::FAdd: |
| 4250 | return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4251 | case Instruction::Sub: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4252 | return SimplifySubInst(LHS, RHS, false, false, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 4253 | case Instruction::FSub: |
| 4254 | return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4255 | case Instruction::Mul: |
| 4256 | return SimplifyMulInst(LHS, RHS, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 4257 | case Instruction::FMul: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4258 | return SimplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4259 | case Instruction::SDiv: |
| 4260 | return SimplifySDivInst(LHS, RHS, Q, MaxRecurse); |
| 4261 | case Instruction::UDiv: |
| 4262 | return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse); |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4263 | case Instruction::FDiv: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4264 | return SimplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4265 | case Instruction::SRem: |
| 4266 | return SimplifySRemInst(LHS, RHS, Q, MaxRecurse); |
| 4267 | case Instruction::URem: |
| 4268 | return SimplifyURemInst(LHS, RHS, Q, MaxRecurse); |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4269 | case Instruction::FRem: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4270 | return SimplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4271 | case Instruction::Shl: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4272 | return SimplifyShlInst(LHS, RHS, false, false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4273 | case Instruction::LShr: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4274 | return SimplifyLShrInst(LHS, RHS, false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4275 | case Instruction::AShr: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4276 | return SimplifyAShrInst(LHS, RHS, false, Q, MaxRecurse); |
| 4277 | case Instruction::And: |
| 4278 | return SimplifyAndInst(LHS, RHS, Q, MaxRecurse); |
| 4279 | case Instruction::Or: |
| 4280 | return SimplifyOrInst(LHS, RHS, Q, MaxRecurse); |
| 4281 | case Instruction::Xor: |
| 4282 | return SimplifyXorInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4283 | default: |
Craig Topper | 8ef20ea | 2017-04-06 18:59:08 +0000 | [diff] [blame] | 4284 | llvm_unreachable("Unexpected opcode"); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4285 | } |
| 4286 | } |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4287 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4288 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 4289 | /// If not, this returns null. |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4290 | /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the |
| 4291 | /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp. |
| 4292 | static Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
| 4293 | const FastMathFlags &FMF, const Query &Q, |
| 4294 | unsigned MaxRecurse) { |
| 4295 | switch (Opcode) { |
| 4296 | case Instruction::FAdd: |
| 4297 | return SimplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 4298 | case Instruction::FSub: |
| 4299 | return SimplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 4300 | case Instruction::FMul: |
| 4301 | return SimplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse); |
Zia Ansari | 394cef8 | 2016-12-08 23:27:40 +0000 | [diff] [blame] | 4302 | case Instruction::FDiv: |
| 4303 | return SimplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4304 | default: |
| 4305 | return SimplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse); |
| 4306 | } |
| 4307 | } |
| 4308 | |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4309 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4310 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4311 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4312 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4313 | return ::SimplifyBinOp(Opcode, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4314 | RecursionLimit); |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4317 | Value *llvm::SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4318 | const FastMathFlags &FMF, const DataLayout &DL, |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4319 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4320 | const DominatorTree *DT, AssumptionCache *AC, |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4321 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4322 | return ::SimplifyFPBinOp(Opcode, LHS, RHS, FMF, Query(DL, TLI, DT, AC, CxtI), |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4323 | RecursionLimit); |
| 4324 | } |
| 4325 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4326 | /// Given operands for a CmpInst, see if we can fold the result. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4327 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4328 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4329 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4330 | return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 4331 | return SimplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4335 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4336 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4337 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4338 | return ::SimplifyCmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4339 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4340 | } |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4341 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4342 | static bool IsIdempotent(Intrinsic::ID ID) { |
| 4343 | switch (ID) { |
| 4344 | default: return false; |
| 4345 | |
| 4346 | // Unary idempotent: f(f(x)) = f(x) |
| 4347 | case Intrinsic::fabs: |
| 4348 | case Intrinsic::floor: |
| 4349 | case Intrinsic::ceil: |
| 4350 | case Intrinsic::trunc: |
| 4351 | case Intrinsic::rint: |
| 4352 | case Intrinsic::nearbyint: |
Hal Finkel | 171817e | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 4353 | case Intrinsic::round: |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4354 | return true; |
| 4355 | } |
| 4356 | } |
| 4357 | |
Peter Collingbourne | 7dd8dbf | 2016-04-22 21:18:02 +0000 | [diff] [blame] | 4358 | static Value *SimplifyRelativeLoad(Constant *Ptr, Constant *Offset, |
| 4359 | const DataLayout &DL) { |
| 4360 | GlobalValue *PtrSym; |
| 4361 | APInt PtrOffset; |
| 4362 | if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL)) |
| 4363 | return nullptr; |
| 4364 | |
| 4365 | Type *Int8PtrTy = Type::getInt8PtrTy(Ptr->getContext()); |
| 4366 | Type *Int32Ty = Type::getInt32Ty(Ptr->getContext()); |
| 4367 | Type *Int32PtrTy = Int32Ty->getPointerTo(); |
| 4368 | Type *Int64Ty = Type::getInt64Ty(Ptr->getContext()); |
| 4369 | |
| 4370 | auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset); |
| 4371 | if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64) |
| 4372 | return nullptr; |
| 4373 | |
| 4374 | uint64_t OffsetInt = OffsetConstInt->getSExtValue(); |
| 4375 | if (OffsetInt % 4 != 0) |
| 4376 | return nullptr; |
| 4377 | |
| 4378 | Constant *C = ConstantExpr::getGetElementPtr( |
| 4379 | Int32Ty, ConstantExpr::getBitCast(Ptr, Int32PtrTy), |
| 4380 | ConstantInt::get(Int64Ty, OffsetInt / 4)); |
| 4381 | Constant *Loaded = ConstantFoldLoadFromConstPtr(C, Int32Ty, DL); |
| 4382 | if (!Loaded) |
| 4383 | return nullptr; |
| 4384 | |
| 4385 | auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded); |
| 4386 | if (!LoadedCE) |
| 4387 | return nullptr; |
| 4388 | |
| 4389 | if (LoadedCE->getOpcode() == Instruction::Trunc) { |
| 4390 | LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4391 | if (!LoadedCE) |
| 4392 | return nullptr; |
| 4393 | } |
| 4394 | |
| 4395 | if (LoadedCE->getOpcode() != Instruction::Sub) |
| 4396 | return nullptr; |
| 4397 | |
| 4398 | auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4399 | if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt) |
| 4400 | return nullptr; |
| 4401 | auto *LoadedLHSPtr = LoadedLHS->getOperand(0); |
| 4402 | |
| 4403 | Constant *LoadedRHS = LoadedCE->getOperand(1); |
| 4404 | GlobalValue *LoadedRHSSym; |
| 4405 | APInt LoadedRHSOffset; |
| 4406 | if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset, |
| 4407 | DL) || |
| 4408 | PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset) |
| 4409 | return nullptr; |
| 4410 | |
| 4411 | return ConstantExpr::getBitCast(LoadedLHSPtr, Int8PtrTy); |
| 4412 | } |
| 4413 | |
David Majnemer | 17a95aa | 2016-07-14 06:58:37 +0000 | [diff] [blame] | 4414 | static bool maskIsAllZeroOrUndef(Value *Mask) { |
| 4415 | auto *ConstMask = dyn_cast<Constant>(Mask); |
| 4416 | if (!ConstMask) |
| 4417 | return false; |
| 4418 | if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask)) |
| 4419 | return true; |
| 4420 | for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E; |
| 4421 | ++I) { |
| 4422 | if (auto *MaskElt = ConstMask->getAggregateElement(I)) |
| 4423 | if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt)) |
| 4424 | continue; |
| 4425 | return false; |
| 4426 | } |
| 4427 | return true; |
| 4428 | } |
| 4429 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4430 | template <typename IterTy> |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4431 | static Value *SimplifyIntrinsic(Function *F, IterTy ArgBegin, IterTy ArgEnd, |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4432 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4433 | Intrinsic::ID IID = F->getIntrinsicID(); |
| 4434 | unsigned NumOperands = std::distance(ArgBegin, ArgEnd); |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4435 | |
| 4436 | // Unary Ops |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4437 | if (NumOperands == 1) { |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4438 | // Perform idempotent optimizations |
| 4439 | if (IsIdempotent(IID)) { |
| 4440 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin)) { |
| 4441 | if (II->getIntrinsicID() == IID) |
| 4442 | return II; |
| 4443 | } |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | switch (IID) { |
| 4447 | case Intrinsic::fabs: { |
| 4448 | if (SignBitMustBeZero(*ArgBegin, Q.TLI)) |
| 4449 | return *ArgBegin; |
Marcello Maggioni | 0616b5f | 2017-01-14 07:28:47 +0000 | [diff] [blame] | 4450 | return nullptr; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4451 | } |
| 4452 | default: |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4453 | return nullptr; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4454 | } |
| 4455 | } |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4456 | |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4457 | // Binary Ops |
| 4458 | if (NumOperands == 2) { |
| 4459 | Value *LHS = *ArgBegin; |
| 4460 | Value *RHS = *(ArgBegin + 1); |
| 4461 | Type *ReturnType = F->getReturnType(); |
| 4462 | |
| 4463 | switch (IID) { |
| 4464 | case Intrinsic::usub_with_overflow: |
| 4465 | case Intrinsic::ssub_with_overflow: { |
| 4466 | // X - X -> { 0, false } |
| 4467 | if (LHS == RHS) |
| 4468 | return Constant::getNullValue(ReturnType); |
| 4469 | |
| 4470 | // X - undef -> undef |
| 4471 | // undef - X -> undef |
| 4472 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) |
| 4473 | return UndefValue::get(ReturnType); |
| 4474 | |
| 4475 | return nullptr; |
| 4476 | } |
| 4477 | case Intrinsic::uadd_with_overflow: |
| 4478 | case Intrinsic::sadd_with_overflow: { |
| 4479 | // X + undef -> undef |
| 4480 | if (isa<UndefValue>(RHS)) |
| 4481 | return UndefValue::get(ReturnType); |
| 4482 | |
| 4483 | return nullptr; |
| 4484 | } |
| 4485 | case Intrinsic::umul_with_overflow: |
| 4486 | case Intrinsic::smul_with_overflow: { |
| 4487 | // X * 0 -> { 0, false } |
| 4488 | if (match(RHS, m_Zero())) |
| 4489 | return Constant::getNullValue(ReturnType); |
| 4490 | |
| 4491 | // X * undef -> { 0, false } |
| 4492 | if (match(RHS, m_Undef())) |
| 4493 | return Constant::getNullValue(ReturnType); |
| 4494 | |
| 4495 | return nullptr; |
| 4496 | } |
| 4497 | case Intrinsic::load_relative: { |
| 4498 | Constant *C0 = dyn_cast<Constant>(LHS); |
| 4499 | Constant *C1 = dyn_cast<Constant>(RHS); |
| 4500 | if (C0 && C1) |
| 4501 | return SimplifyRelativeLoad(C0, C1, Q.DL); |
| 4502 | return nullptr; |
| 4503 | } |
| 4504 | default: |
| 4505 | return nullptr; |
| 4506 | } |
| 4507 | } |
| 4508 | |
| 4509 | // Simplify calls to llvm.masked.load.* |
| 4510 | switch (IID) { |
| 4511 | case Intrinsic::masked_load: { |
| 4512 | Value *MaskArg = ArgBegin[2]; |
| 4513 | Value *PassthruArg = ArgBegin[3]; |
| 4514 | // If the mask is all zeros or undef, the "passthru" argument is the result. |
| 4515 | if (maskIsAllZeroOrUndef(MaskArg)) |
| 4516 | return PassthruArg; |
| 4517 | return nullptr; |
| 4518 | } |
| 4519 | default: |
| 4520 | return nullptr; |
| 4521 | } |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4524 | template <typename IterTy> |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4525 | static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd, |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4526 | const Query &Q, unsigned MaxRecurse) { |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4527 | Type *Ty = V->getType(); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4528 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) |
| 4529 | Ty = PTy->getElementType(); |
| 4530 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 4531 | |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4532 | // call undef -> undef |
David Majnemer | bb53d23 | 2016-06-25 07:37:30 +0000 | [diff] [blame] | 4533 | // call null -> undef |
| 4534 | if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V)) |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4535 | return UndefValue::get(FTy->getReturnType()); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4536 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4537 | Function *F = dyn_cast<Function>(V); |
| 4538 | if (!F) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4539 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4540 | |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4541 | if (F->isIntrinsic()) |
| 4542 | if (Value *Ret = SimplifyIntrinsic(F, ArgBegin, ArgEnd, Q, MaxRecurse)) |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4543 | return Ret; |
| 4544 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4545 | if (!canConstantFoldCallTo(F)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4546 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4547 | |
| 4548 | SmallVector<Constant *, 4> ConstantArgs; |
| 4549 | ConstantArgs.reserve(ArgEnd - ArgBegin); |
| 4550 | for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) { |
| 4551 | Constant *C = dyn_cast<Constant>(*I); |
| 4552 | if (!C) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4553 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4554 | ConstantArgs.push_back(C); |
| 4555 | } |
| 4556 | |
| 4557 | return ConstantFoldCall(F, ConstantArgs, Q.TLI); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4560 | Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4561 | User::op_iterator ArgEnd, const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4562 | const TargetLibraryInfo *TLI, const DominatorTree *DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4563 | AssumptionCache *AC, const Instruction *CxtI) { |
| 4564 | return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4565 | RecursionLimit); |
| 4566 | } |
| 4567 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4568 | Value *llvm::SimplifyCall(Value *V, ArrayRef<Value *> Args, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4569 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4570 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4571 | const Instruction *CxtI) { |
| 4572 | return ::SimplifyCall(V, Args.begin(), Args.end(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4573 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4576 | /// See if we can compute a simplified version of this instruction. |
| 4577 | /// If not, this returns null. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4578 | Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 4579 | const TargetLibraryInfo *TLI, |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 4580 | const DominatorTree *DT, AssumptionCache *AC, |
| 4581 | OptimizationRemarkEmitter *ORE) { |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4582 | Value *Result; |
| 4583 | |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4584 | switch (I->getOpcode()) { |
| 4585 | default: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 4586 | Result = ConstantFoldInstruction(I, DL, TLI); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4587 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4588 | case Instruction::FAdd: |
| 4589 | Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4590 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4591 | break; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 4592 | case Instruction::Add: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4593 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 4594 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4595 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4596 | TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4597 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4598 | case Instruction::FSub: |
| 4599 | Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4600 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4601 | break; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4602 | case Instruction::Sub: |
| 4603 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 4604 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4605 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4606 | TLI, DT, AC, I); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4607 | break; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4608 | case Instruction::FMul: |
| 4609 | Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4610 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4611 | break; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4612 | case Instruction::Mul: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4613 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4614 | SimplifyMulInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4615 | break; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4616 | case Instruction::SDiv: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4617 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4618 | AC, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4619 | break; |
| 4620 | case Instruction::UDiv: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4621 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4622 | AC, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4623 | break; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4624 | case Instruction::FDiv: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4625 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4626 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4627 | break; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4628 | case Instruction::SRem: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4629 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4630 | AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4631 | break; |
| 4632 | case Instruction::URem: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4633 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4634 | AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4635 | break; |
| 4636 | case Instruction::FRem: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4637 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4638 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4639 | break; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4640 | case Instruction::Shl: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4641 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 4642 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4643 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4644 | TLI, DT, AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4645 | break; |
| 4646 | case Instruction::LShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4647 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4648 | cast<BinaryOperator>(I)->isExact(), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4649 | AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4650 | break; |
| 4651 | case Instruction::AShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4652 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4653 | cast<BinaryOperator>(I)->isExact(), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4654 | AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4655 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4656 | case Instruction::And: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4657 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4658 | SimplifyAndInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4659 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4660 | case Instruction::Or: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4661 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4662 | SimplifyOrInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4663 | break; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4664 | case Instruction::Xor: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4665 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4666 | SimplifyXorInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4667 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4668 | case Instruction::ICmp: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4669 | Result = |
| 4670 | SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), I->getOperand(0), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4671 | I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4672 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4673 | case Instruction::FCmp: |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 4674 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
| 4675 | I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4676 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4677 | break; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 4678 | case Instruction::Select: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4679 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4680 | I->getOperand(2), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4681 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4682 | case Instruction::GetElementPtr: { |
| 4683 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 4684 | Result = SimplifyGEPInst(cast<GetElementPtrInst>(I)->getSourceElementType(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4685 | Ops, DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4686 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4687 | } |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4688 | case Instruction::InsertValue: { |
| 4689 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 4690 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 4691 | IV->getInsertedValueOperand(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4692 | IV->getIndices(), DL, TLI, DT, AC, I); |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4693 | break; |
| 4694 | } |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 4695 | case Instruction::ExtractValue: { |
| 4696 | auto *EVI = cast<ExtractValueInst>(I); |
| 4697 | Result = SimplifyExtractValueInst(EVI->getAggregateOperand(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4698 | EVI->getIndices(), DL, TLI, DT, AC, I); |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 4699 | break; |
| 4700 | } |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4701 | case Instruction::ExtractElement: { |
| 4702 | auto *EEI = cast<ExtractElementInst>(I); |
| 4703 | Result = SimplifyExtractElementInst( |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4704 | EEI->getVectorOperand(), EEI->getIndexOperand(), DL, TLI, DT, AC, I); |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4705 | break; |
| 4706 | } |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4707 | case Instruction::ShuffleVector: { |
| 4708 | auto *SVI = cast<ShuffleVectorInst>(I); |
| 4709 | Result = SimplifyShuffleVectorInst(SVI->getOperand(0), SVI->getOperand(1), |
| 4710 | SVI->getMask(), SVI->getType(), DL, TLI, |
| 4711 | DT, AC, I); |
| 4712 | break; |
| 4713 | } |
Duncan Sands | 4581ddc | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 4714 | case Instruction::PHI: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4715 | Result = SimplifyPHINode(cast<PHINode>(I), Query(DL, TLI, DT, AC, I)); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4716 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4717 | case Instruction::Call: { |
| 4718 | CallSite CS(cast<CallInst>(I)); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4719 | Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4720 | TLI, DT, AC, I); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4721 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4722 | } |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4723 | #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc: |
| 4724 | #include "llvm/IR/Instruction.def" |
| 4725 | #undef HANDLE_CAST_INST |
| 4726 | Result = SimplifyCastInst(I->getOpcode(), I->getOperand(0), I->getType(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4727 | DL, TLI, DT, AC, I); |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4728 | break; |
Craig Topper | 81c03a7 | 2017-04-12 22:54:24 +0000 | [diff] [blame] | 4729 | case Instruction::Alloca: |
| 4730 | // No simplifications for Alloca and it can't be constant folded. |
| 4731 | Result = nullptr; |
| 4732 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4733 | } |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4734 | |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4735 | // In general, it is possible for computeKnownBits to determine all bits in a |
| 4736 | // value even when the operands are not all constants. |
Sanjay Patel | 8ca30ab | 2016-11-27 21:07:28 +0000 | [diff] [blame] | 4737 | if (!Result && I->getType()->isIntOrIntVectorTy()) { |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4738 | unsigned BitWidth = I->getType()->getScalarSizeInBits(); |
| 4739 | APInt KnownZero(BitWidth, 0); |
| 4740 | APInt KnownOne(BitWidth, 0); |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 4741 | computeKnownBits(I, KnownZero, KnownOne, DL, /*Depth*/0, AC, I, DT, ORE); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4742 | if ((KnownZero | KnownOne).isAllOnesValue()) |
Sanjay Patel | 8ca30ab | 2016-11-27 21:07:28 +0000 | [diff] [blame] | 4743 | Result = ConstantInt::get(I->getType(), KnownOne); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4744 | } |
| 4745 | |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4746 | /// If called on unreachable code, the above logic may report that the |
| 4747 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 4748 | /// detecting that case here, returning a safe value instead. |
| 4749 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4750 | } |
| 4751 | |
Sanjay Patel | f44bd38 | 2016-01-20 18:59:48 +0000 | [diff] [blame] | 4752 | /// \brief Implementation of recursive simplification through an instruction's |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4753 | /// uses. |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4754 | /// |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4755 | /// This is the common implementation of the recursive simplification routines. |
| 4756 | /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to |
| 4757 | /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of |
| 4758 | /// instructions to process and attempt to simplify it using |
| 4759 | /// InstructionSimplify. |
| 4760 | /// |
| 4761 | /// This routine returns 'true' only when *it* simplifies something. The passed |
| 4762 | /// in simplified value does not count toward this. |
| 4763 | static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4764 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4765 | const DominatorTree *DT, |
| 4766 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4767 | bool Simplified = false; |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4768 | SmallSetVector<Instruction *, 8> Worklist; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4769 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4770 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4771 | // If we have an explicit value to collapse to, do that round of the |
| 4772 | // simplification loop by hand initially. |
| 4773 | if (SimpleV) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4774 | for (User *U : I->users()) |
| 4775 | if (U != I) |
| 4776 | Worklist.insert(cast<Instruction>(U)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4777 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4778 | // Replace the instruction with its simplified value. |
| 4779 | I->replaceAllUsesWith(SimpleV); |
Chris Lattner | 19eff2a | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 4780 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4781 | // Gracefully handle edge cases where the instruction is not wired into any |
| 4782 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 4783 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 4784 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4785 | I->eraseFromParent(); |
| 4786 | } else { |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4787 | Worklist.insert(I); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4788 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4789 | |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4790 | // Note that we must test the size on each iteration, the worklist can grow. |
| 4791 | for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { |
| 4792 | I = Worklist[Idx]; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4793 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4794 | // See if this instruction simplifies. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4795 | SimpleV = SimplifyInstruction(I, DL, TLI, DT, AC); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4796 | if (!SimpleV) |
| 4797 | continue; |
| 4798 | |
| 4799 | Simplified = true; |
| 4800 | |
| 4801 | // Stash away all the uses of the old instruction so we can check them for |
| 4802 | // recursive simplifications after a RAUW. This is cheaper than checking all |
| 4803 | // uses of To on the recursive step in most cases. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4804 | for (User *U : I->users()) |
| 4805 | Worklist.insert(cast<Instruction>(U)); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4806 | |
| 4807 | // Replace the instruction with its simplified value. |
| 4808 | I->replaceAllUsesWith(SimpleV); |
| 4809 | |
| 4810 | // Gracefully handle edge cases where the instruction is not wired into any |
| 4811 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 4812 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 4813 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4814 | I->eraseFromParent(); |
| 4815 | } |
| 4816 | return Simplified; |
| 4817 | } |
| 4818 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4819 | bool llvm::recursivelySimplifyInstruction(Instruction *I, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4820 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4821 | const DominatorTree *DT, |
| 4822 | AssumptionCache *AC) { |
| 4823 | return replaceAndRecursivelySimplifyImpl(I, nullptr, TLI, DT, AC); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4824 | } |
| 4825 | |
| 4826 | bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4827 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4828 | const DominatorTree *DT, |
| 4829 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4830 | assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!"); |
| 4831 | assert(SimpleV && "Must provide a simplified value."); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4832 | return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4833 | } |