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" |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/AssumptionCache.h" |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/CaptureTracking.h" |
Craig Topper | 0aa3a19 | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/CmpInstAnalysis.h" |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ConstantFolding.h" |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/LoopAnalysisManager.h" |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/ValueTracking.h" |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/VectorUtils.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 32 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 35 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 36 | #include "llvm/IR/GlobalAlias.h" |
| 37 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 38 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 39 | #include "llvm/IR/ValueHandle.h" |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 40 | #include "llvm/Support/KnownBits.h" |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 41 | #include <algorithm> |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 42 | using namespace llvm; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 43 | using namespace llvm::PatternMatch; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 44 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 45 | #define DEBUG_TYPE "instsimplify" |
| 46 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 47 | enum { RecursionLimit = 3 }; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 48 | |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 49 | STATISTIC(NumExpand, "Number of expansions"); |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 50 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 51 | |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 52 | static Value *SimplifyAndInst(Value *, Value *, const SimplifyQuery &, unsigned); |
| 53 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 54 | unsigned); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 55 | static Value *SimplifyFPBinOp(unsigned, Value *, Value *, const FastMathFlags &, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 56 | const SimplifyQuery &, unsigned); |
| 57 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const SimplifyQuery &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 58 | unsigned); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 59 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 60 | const SimplifyQuery &Q, unsigned MaxRecurse); |
| 61 | static Value *SimplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned); |
| 62 | static Value *SimplifyXorInst(Value *, Value *, const SimplifyQuery &, unsigned); |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 63 | static Value *SimplifyCastInst(unsigned, Value *, Type *, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 64 | const SimplifyQuery &, unsigned); |
George Burgess IV | 8e807bf | 2018-04-24 00:25:01 +0000 | [diff] [blame] | 65 | static Value *SimplifyGEPInst(Type *, ArrayRef<Value *>, const SimplifyQuery &, |
| 66 | unsigned); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 67 | |
Sanjay Patel | 35ed241 | 2017-04-16 17:43:11 +0000 | [diff] [blame] | 68 | /// For a boolean type or a vector of boolean type, return false or a vector |
| 69 | /// with every element false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 70 | static Constant *getFalse(Type *Ty) { |
Sanjay Patel | 35ed241 | 2017-04-16 17:43:11 +0000 | [diff] [blame] | 71 | return ConstantInt::getFalse(Ty); |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Sanjay Patel | 35ed241 | 2017-04-16 17:43:11 +0000 | [diff] [blame] | 74 | /// For a boolean type or a vector of boolean type, return true or a vector |
| 75 | /// with every element true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 76 | static Constant *getTrue(Type *Ty) { |
Sanjay Patel | 35ed241 | 2017-04-16 17:43:11 +0000 | [diff] [blame] | 77 | return ConstantInt::getTrue(Ty); |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 80 | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
| 81 | static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, |
| 82 | Value *RHS) { |
| 83 | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
| 84 | if (!Cmp) |
| 85 | return false; |
| 86 | CmpInst::Predicate CPred = Cmp->getPredicate(); |
| 87 | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
| 88 | if (CPred == Pred && CLHS == LHS && CRHS == RHS) |
| 89 | return true; |
| 90 | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && |
| 91 | CRHS == LHS; |
| 92 | } |
| 93 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 94 | /// Does the given value dominate the specified phi node? |
Sanjay Patel | 5da361a | 2018-04-10 18:38:19 +0000 | [diff] [blame] | 95 | static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 96 | Instruction *I = dyn_cast<Instruction>(V); |
| 97 | if (!I) |
| 98 | // Arguments and constants dominate all instructions. |
| 99 | return true; |
| 100 | |
Chandler Carruth | 3ffccb3 | 2012-03-21 10:58:47 +0000 | [diff] [blame] | 101 | // If we are processing instructions (and/or basic blocks) that have not been |
| 102 | // fully added to a function, the parent nodes may still be null. Simply |
| 103 | // return the conservative answer in these cases. |
Sanjay Patel | 5da361a | 2018-04-10 18:38:19 +0000 | [diff] [blame] | 104 | if (!I->getParent() || !P->getParent() || !I->getFunction()) |
Chandler Carruth | 3ffccb3 | 2012-03-21 10:58:47 +0000 | [diff] [blame] | 105 | return false; |
| 106 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 107 | // If we have a DominatorTree then do a precise test. |
Daniel Berlin | 71ff663 | 2017-05-31 01:47:24 +0000 | [diff] [blame] | 108 | if (DT) |
Eli Friedman | c8cbd06 | 2012-03-13 01:06:07 +0000 | [diff] [blame] | 109 | return DT->dominates(I, P); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 110 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 111 | // Otherwise, if the instruction is in the entry block and is not an invoke, |
| 112 | // then it obviously dominates all phi nodes. |
Sanjay Patel | 5da361a | 2018-04-10 18:38:19 +0000 | [diff] [blame] | 113 | if (I->getParent() == &I->getFunction()->getEntryBlock() && |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 114 | !isa<InvokeInst>(I)) |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 115 | return true; |
| 116 | |
| 117 | return false; |
| 118 | } |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 119 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 120 | /// Simplify "A op (B op' C)" by distributing op over op', turning it into |
| 121 | /// "(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] | 122 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 123 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 124 | /// Returns the simplified value, or null if no simplification was performed. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 125 | static Value *ExpandBinOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, |
Craig Topper | 9c913bf | 2017-05-19 16:56:53 +0000 | [diff] [blame] | 126 | Instruction::BinaryOps OpcodeToExpand, |
| 127 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 128 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 129 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 130 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 131 | |
| 132 | // Check whether the expression has the form "(A op' B) op C". |
| 133 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 134 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 135 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 136 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 137 | // Do "A op C" and "B op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 138 | if (Value *L = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) |
| 139 | if (Value *R = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 140 | // They do! Return "L op' R" if it simplifies or is already available. |
| 141 | // 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] | 142 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 143 | && L == B && R == A)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 144 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 145 | return LHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 146 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 147 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 148 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 149 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 150 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 151 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | // Check whether the expression has the form "A op (B op' C)". |
| 156 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 157 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 158 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 159 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 160 | // Do "A op B" and "A op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 161 | if (Value *L = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) |
| 162 | if (Value *R = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 163 | // They do! Return "L op' R" if it simplifies or is already available. |
| 164 | // 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] | 165 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 166 | && L == C && R == B)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 167 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 168 | return RHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 169 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 170 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 171 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 172 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 173 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 174 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 178 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 181 | /// Generic simplifications for associative binary operations. |
| 182 | /// Returns the simpler value, or null if none was found. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 183 | static Value *SimplifyAssociativeBinOp(Instruction::BinaryOps Opcode, |
Craig Topper | 9c913bf | 2017-05-19 16:56:53 +0000 | [diff] [blame] | 184 | Value *LHS, Value *RHS, |
| 185 | const SimplifyQuery &Q, |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 186 | unsigned MaxRecurse) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 187 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 188 | |
| 189 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 190 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 191 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 192 | |
| 193 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 194 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 195 | |
| 196 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 197 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 198 | Value *A = Op0->getOperand(0); |
| 199 | Value *B = Op0->getOperand(1); |
| 200 | Value *C = RHS; |
| 201 | |
| 202 | // Does "B op C" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 203 | if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 204 | // It does! Return "A op V" if it simplifies or is already available. |
| 205 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 206 | if (V == B) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 207 | // Otherwise return "A op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 208 | if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 209 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 210 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 211 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 216 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 217 | Value *A = LHS; |
| 218 | Value *B = Op1->getOperand(0); |
| 219 | Value *C = Op1->getOperand(1); |
| 220 | |
| 221 | // Does "A op B" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 222 | if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 223 | // It does! Return "V op C" if it simplifies or is already available. |
| 224 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 225 | if (V == B) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 226 | // Otherwise return "V op C" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 227 | if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 228 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 229 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 230 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | // The remaining transforms require commutativity as well as associativity. |
| 235 | if (!Instruction::isCommutative(Opcode)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 236 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 237 | |
| 238 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 239 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 240 | Value *A = Op0->getOperand(0); |
| 241 | Value *B = Op0->getOperand(1); |
| 242 | Value *C = RHS; |
| 243 | |
| 244 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 245 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 246 | // It does! Return "V op B" if it simplifies or is already available. |
| 247 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 248 | if (V == A) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 249 | // Otherwise return "V op B" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 250 | if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 251 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 252 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 253 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 258 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 259 | Value *A = LHS; |
| 260 | Value *B = Op1->getOperand(0); |
| 261 | Value *C = Op1->getOperand(1); |
| 262 | |
| 263 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 264 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 265 | // It does! Return "B op V" if it simplifies or is already available. |
| 266 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 267 | if (V == C) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 268 | // Otherwise return "B op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 269 | if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 270 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 271 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 272 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 276 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 279 | /// In the case of a binary operation with a select instruction as an operand, |
| 280 | /// try to simplify the binop by seeing whether evaluating it on both branches |
| 281 | /// of the select results in the same value. Returns the common value if so, |
| 282 | /// otherwise returns null. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 283 | static Value *ThreadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 284 | Value *RHS, const SimplifyQuery &Q, |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 285 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 286 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 287 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 288 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 289 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 290 | SelectInst *SI; |
| 291 | if (isa<SelectInst>(LHS)) { |
| 292 | SI = cast<SelectInst>(LHS); |
| 293 | } else { |
| 294 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 295 | SI = cast<SelectInst>(RHS); |
| 296 | } |
| 297 | |
| 298 | // Evaluate the BinOp on the true and false branches of the select. |
| 299 | Value *TV; |
| 300 | Value *FV; |
| 301 | if (SI == LHS) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 302 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse); |
| 303 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 304 | } else { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 305 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse); |
| 306 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Duncan Sands | e3c5395 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 309 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 310 | // If they both failed to simplify then return null. |
| 311 | if (TV == FV) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 312 | return TV; |
| 313 | |
| 314 | // If one branch simplified to undef, return the other one. |
| 315 | if (TV && isa<UndefValue>(TV)) |
| 316 | return FV; |
| 317 | if (FV && isa<UndefValue>(FV)) |
| 318 | return TV; |
| 319 | |
| 320 | // If applying the operation did not change the true and false select values, |
| 321 | // then the result of the binop is the select itself. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 322 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 323 | return SI; |
| 324 | |
| 325 | // If one branch simplified and the other did not, and the simplified |
| 326 | // value is equal to the unsimplified one, return the simplified value. |
| 327 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 328 | if ((FV && !TV) || (TV && !FV)) { |
| 329 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 330 | // same as the original operation. |
| 331 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
Zachary Turner | 260fe3e | 2017-12-14 22:07:03 +0000 | [diff] [blame] | 332 | if (Simplified && Simplified->getOpcode() == unsigned(Opcode)) { |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 333 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 334 | // We already know that "op" is the same as for the simplified value. See |
| 335 | // if the operands match too. If so, return the simplified value. |
| 336 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 337 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 338 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 339 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 340 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 341 | return Simplified; |
| 342 | if (Simplified->isCommutative() && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 343 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 344 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 345 | return Simplified; |
| 346 | } |
| 347 | } |
| 348 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 349 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 352 | /// In the case of a comparison with a select instruction, try to simplify the |
| 353 | /// comparison by seeing whether both branches of the select result in the same |
| 354 | /// value. Returns the common value if so, otherwise returns null. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 355 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 356 | Value *RHS, const SimplifyQuery &Q, |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 357 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 358 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 359 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 360 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 361 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 362 | // Make sure the select is on the LHS. |
| 363 | if (!isa<SelectInst>(LHS)) { |
| 364 | std::swap(LHS, RHS); |
| 365 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 366 | } |
| 367 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 368 | SelectInst *SI = cast<SelectInst>(LHS); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 369 | Value *Cond = SI->getCondition(); |
| 370 | Value *TV = SI->getTrueValue(); |
| 371 | Value *FV = SI->getFalseValue(); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 372 | |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 373 | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 374 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 375 | Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 376 | if (TCmp == Cond) { |
| 377 | // It not only simplified, it simplified to the select condition. Replace |
| 378 | // it with 'true'. |
| 379 | TCmp = getTrue(Cond->getType()); |
| 380 | } else if (!TCmp) { |
| 381 | // It didn't simplify. However if "cmp TV, RHS" is equal to the select |
| 382 | // condition then we can replace it with 'true'. Otherwise give up. |
| 383 | if (!isSameCompare(Cond, Pred, TV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 384 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 385 | TCmp = getTrue(Cond->getType()); |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 388 | // Does "cmp FV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 389 | Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 390 | if (FCmp == Cond) { |
| 391 | // It not only simplified, it simplified to the select condition. Replace |
| 392 | // it with 'false'. |
| 393 | FCmp = getFalse(Cond->getType()); |
| 394 | } else if (!FCmp) { |
| 395 | // It didn't simplify. However if "cmp FV, RHS" is equal to the select |
| 396 | // condition then we can replace it with 'false'. Otherwise give up. |
| 397 | if (!isSameCompare(Cond, Pred, FV, 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 | FCmp = getFalse(Cond->getType()); |
| 400 | } |
| 401 | |
| 402 | // If both sides simplified to the same value, then use it as the result of |
| 403 | // the original comparison. |
| 404 | if (TCmp == FCmp) |
| 405 | return TCmp; |
Duncan Sands | 26641d7 | 2012-02-10 14:31:24 +0000 | [diff] [blame] | 406 | |
| 407 | // The remaining cases only make sense if the select condition has the same |
| 408 | // type as the result of the comparison, so bail out if this is not so. |
| 409 | if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy()) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 410 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 411 | // If the false value simplified to false, then the result of the compare |
| 412 | // is equal to "Cond && TCmp". This also catches the case when the false |
| 413 | // value simplified to false and the true value to true, returning "Cond". |
| 414 | if (match(FCmp, m_Zero())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 415 | if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 416 | return V; |
| 417 | // If the true value simplified to true, then the result of the compare |
| 418 | // is equal to "Cond || FCmp". |
| 419 | if (match(TCmp, m_One())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 420 | if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 421 | return V; |
| 422 | // Finally, if the false value simplified to true and the true value to |
| 423 | // false, then the result of the compare is equal to "!Cond". |
| 424 | if (match(FCmp, m_One()) && match(TCmp, m_Zero())) |
| 425 | if (Value *V = |
| 426 | SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 427 | Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 428 | return V; |
| 429 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 430 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 433 | /// In the case of a binary operation with an operand that is a PHI instruction, |
| 434 | /// try to simplify the binop by seeing whether evaluating it on the incoming |
| 435 | /// phi values yields the same result for every value. If so returns the common |
| 436 | /// value, otherwise returns null. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 437 | static Value *ThreadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 438 | Value *RHS, const SimplifyQuery &Q, |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 439 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 440 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 441 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 442 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 443 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 444 | PHINode *PI; |
| 445 | if (isa<PHINode>(LHS)) { |
| 446 | PI = cast<PHINode>(LHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 447 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
Sanjay Patel | 5da361a | 2018-04-10 18:38:19 +0000 | [diff] [blame] | 448 | if (!valueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 449 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 450 | } else { |
| 451 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 452 | PI = cast<PHINode>(RHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 453 | // Bail out if LHS and the phi may be mutually interdependent due to a loop. |
Sanjay Patel | 5da361a | 2018-04-10 18:38:19 +0000 | [diff] [blame] | 454 | if (!valueDominatesPHI(LHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 455 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 459 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 460 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 461 | // 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] | 462 | if (Incoming == PI) continue; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 463 | Value *V = PI == LHS ? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 464 | SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) : |
| 465 | SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 466 | // If the operation failed to simplify, or simplified to a different value |
| 467 | // to previously, then give up. |
| 468 | if (!V || (CommonValue && V != CommonValue)) |
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 | CommonValue = V; |
| 471 | } |
| 472 | |
| 473 | return CommonValue; |
| 474 | } |
| 475 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 476 | /// In the case of a comparison with a PHI instruction, try to simplify the |
| 477 | /// comparison by seeing whether comparing with all of the incoming phi values |
| 478 | /// yields the same result every time. If so returns the common result, |
| 479 | /// otherwise returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 480 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 481 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 482 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 483 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 484 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 485 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 486 | // Make sure the phi is on the LHS. |
| 487 | if (!isa<PHINode>(LHS)) { |
| 488 | std::swap(LHS, RHS); |
| 489 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 490 | } |
| 491 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 492 | PHINode *PI = cast<PHINode>(LHS); |
| 493 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 494 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
Sanjay Patel | 5da361a | 2018-04-10 18:38:19 +0000 | [diff] [blame] | 495 | if (!valueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 496 | return nullptr; |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 497 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 498 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 499 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 500 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 501 | // 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] | 502 | if (Incoming == PI) continue; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 503 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 504 | // If the operation failed to simplify, or simplified to a different value |
| 505 | // to previously, then give up. |
| 506 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 507 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 508 | CommonValue = V; |
| 509 | } |
| 510 | |
| 511 | return CommonValue; |
| 512 | } |
| 513 | |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 514 | static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode, |
| 515 | Value *&Op0, Value *&Op1, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 516 | const SimplifyQuery &Q) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 517 | if (auto *CLHS = dyn_cast<Constant>(Op0)) { |
| 518 | if (auto *CRHS = dyn_cast<Constant>(Op1)) |
| 519 | return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL); |
| 520 | |
| 521 | // Canonicalize the constant to the RHS if this is a commutative operation. |
| 522 | if (Instruction::isCommutative(Opcode)) |
| 523 | std::swap(Op0, Op1); |
| 524 | } |
| 525 | return nullptr; |
| 526 | } |
| 527 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 528 | /// Given operands for an Add, see if we can fold the result. |
| 529 | /// If not, this returns null. |
Roman Lebedev | f87321a | 2018-06-08 15:44:53 +0000 | [diff] [blame] | 530 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 531 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 532 | if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q)) |
| 533 | return C; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 534 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 535 | // X + undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 536 | if (match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 537 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 538 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 539 | // X + 0 -> X |
| 540 | if (match(Op1, m_Zero())) |
| 541 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 542 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 543 | // X + (Y - X) -> Y |
| 544 | // (Y - X) + X -> Y |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 545 | // Eg: X + -X -> 0 |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 546 | Value *Y = nullptr; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 547 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 548 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 549 | return Y; |
| 550 | |
| 551 | // X + ~X -> -1 since ~X = -X-1 |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 552 | Type *Ty = Op0->getType(); |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 553 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 554 | match(Op1, m_Not(m_Specific(Op0)))) |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 555 | return Constant::getAllOnesValue(Ty); |
| 556 | |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 557 | // add nsw/nuw (xor Y, signmask), signmask --> Y |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 558 | // The no-wrapping add guarantees that the top bit will be set by the add. |
| 559 | // Therefore, the xor must be clearing the already set sign bit of Y. |
Roman Lebedev | f87321a | 2018-06-08 15:44:53 +0000 | [diff] [blame] | 560 | if ((IsNSW || IsNUW) && match(Op1, m_SignMask()) && |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 561 | match(Op0, m_Xor(m_Value(Y), m_SignMask()))) |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 562 | return Y; |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 563 | |
Roman Lebedev | b060ce4 | 2018-06-08 15:44:47 +0000 | [diff] [blame] | 564 | // add nuw %x, -1 -> -1, because %x can only be 0. |
Roman Lebedev | f87321a | 2018-06-08 15:44:53 +0000 | [diff] [blame] | 565 | if (IsNUW && match(Op1, m_AllOnes())) |
Roman Lebedev | b060ce4 | 2018-06-08 15:44:47 +0000 | [diff] [blame] | 566 | return Op1; // Which is -1. |
| 567 | |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 568 | /// i1 add -> xor. |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 569 | if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 570 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 571 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 572 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 573 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 574 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 575 | MaxRecurse)) |
| 576 | return V; |
| 577 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 578 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 579 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 580 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 581 | // only if B and C are equal. If B and C are equal then (since we assume |
| 582 | // that operands have already been simplified) "select(cond, B, C)" should |
| 583 | // have been simplified to the common value of B and C already. Analysing |
| 584 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 585 | // for threading over phi nodes. |
| 586 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 587 | return nullptr; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Roman Lebedev | f87321a | 2018-06-08 15:44:53 +0000 | [diff] [blame] | 590 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 591 | const SimplifyQuery &Query) { |
Roman Lebedev | f87321a | 2018-06-08 15:44:53 +0000 | [diff] [blame] | 592 | return ::SimplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query, RecursionLimit); |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 595 | /// Compute the base pointer and cumulative constant offsets for V. |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 596 | /// |
| 597 | /// This strips all constant offsets off of V, leaving it the base pointer, and |
| 598 | /// accumulates the total constant offset applied in the returned constant. It |
| 599 | /// returns 0 if V is not a pointer, and returns the constant '0' if there are |
| 600 | /// no constant offsets applied. |
Dan Gohman | 36fa839 | 2013-01-31 02:45:26 +0000 | [diff] [blame] | 601 | /// |
| 602 | /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't |
| 603 | /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc. |
| 604 | /// folding. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 605 | static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V, |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 606 | bool AllowNonInbounds = false) { |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 607 | assert(V->getType()->isPtrOrPtrVectorTy()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 608 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 609 | Type *IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType(); |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 610 | APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 611 | |
| 612 | // Even though we don't look through PHI nodes, we could be called on an |
| 613 | // instruction in an unreachable block, which may be on a cycle. |
| 614 | SmallPtrSet<Value *, 4> Visited; |
| 615 | Visited.insert(V); |
| 616 | do { |
| 617 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 618 | if ((!AllowNonInbounds && !GEP->isInBounds()) || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 619 | !GEP->accumulateConstantOffset(DL, Offset)) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 620 | break; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 621 | V = GEP->getPointerOperand(); |
| 622 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 623 | V = cast<Operator>(V)->getOperand(0); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 624 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 625 | if (GA->isInterposable()) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 626 | break; |
| 627 | V = GA->getAliasee(); |
| 628 | } else { |
Hal Finkel | 2cac58f | 2016-07-11 03:37:59 +0000 | [diff] [blame] | 629 | if (auto CS = CallSite(V)) |
| 630 | if (Value *RV = CS.getReturnedArgOperand()) { |
| 631 | V = RV; |
| 632 | continue; |
| 633 | } |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 634 | break; |
| 635 | } |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 636 | assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!"); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 637 | } while (Visited.insert(V).second); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 638 | |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 639 | Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset); |
| 640 | if (V->getType()->isVectorTy()) |
| 641 | return ConstantVector::getSplat(V->getType()->getVectorNumElements(), |
| 642 | OffsetIntPtr); |
| 643 | return OffsetIntPtr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 646 | /// Compute the constant difference between two pointer values. |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 647 | /// If the difference is not a constant, returns zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 648 | static Constant *computePointerDifference(const DataLayout &DL, Value *LHS, |
| 649 | Value *RHS) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 650 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 651 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 652 | |
| 653 | // If LHS and RHS are not related via constant offsets to the same base |
| 654 | // value, there is nothing we can do here. |
| 655 | if (LHS != RHS) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 656 | return nullptr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 657 | |
| 658 | // Otherwise, the difference of LHS - RHS can be computed as: |
| 659 | // LHS - RHS |
| 660 | // = (LHSOffset + Base) - (RHSOffset + Base) |
| 661 | // = LHSOffset - RHSOffset |
| 662 | return ConstantExpr::getSub(LHSOffset, RHSOffset); |
| 663 | } |
| 664 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 665 | /// Given operands for a Sub, see if we can fold the result. |
| 666 | /// If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 667 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 668 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 669 | if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q)) |
| 670 | return C; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 671 | |
| 672 | // X - undef -> undef |
| 673 | // undef - X -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 674 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 675 | return UndefValue::get(Op0->getType()); |
| 676 | |
| 677 | // X - 0 -> X |
| 678 | if (match(Op1, m_Zero())) |
| 679 | return Op0; |
| 680 | |
| 681 | // X - X -> 0 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 682 | if (Op0 == Op1) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 683 | return Constant::getNullValue(Op0->getType()); |
| 684 | |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 685 | // Is this a negation? |
| 686 | if (match(Op0, m_Zero())) { |
| 687 | // 0 - X -> 0 if the sub is NUW. |
| 688 | if (isNUW) |
Sanjay Patel | 30be665 | 2018-04-22 17:07:44 +0000 | [diff] [blame] | 689 | return Constant::getNullValue(Op0->getType()); |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 690 | |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 691 | KnownBits Known = computeKnownBits(Op1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 692 | if (Known.Zero.isMaxSignedValue()) { |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 693 | // Op1 is either 0 or the minimum signed value. If the sub is NSW, then |
| 694 | // Op1 must be 0 because negating the minimum signed value is undefined. |
| 695 | if (isNSW) |
Sanjay Patel | 30be665 | 2018-04-22 17:07:44 +0000 | [diff] [blame] | 696 | return Constant::getNullValue(Op0->getType()); |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 697 | |
| 698 | // 0 - X -> X if X is 0 or the minimum signed value. |
| 699 | return Op1; |
| 700 | } |
| 701 | } |
David Majnemer | cd4fbcd | 2014-07-31 04:49:18 +0000 | [diff] [blame] | 702 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 703 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 704 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
Dinesh Dwivedi | 99281a0 | 2014-06-26 08:57:33 +0000 | [diff] [blame] | 705 | Value *X = nullptr, *Y = nullptr, *Z = Op1; |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 706 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 707 | // See if "V === Y - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 708 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 709 | // It does! Now see if "X + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 710 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 711 | // It does, we successfully reassociated! |
| 712 | ++NumReassoc; |
| 713 | return W; |
| 714 | } |
| 715 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 716 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 717 | // It does! Now see if "Y + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 718 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 719 | // It does, we successfully reassociated! |
| 720 | ++NumReassoc; |
| 721 | return W; |
| 722 | } |
| 723 | } |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 724 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 725 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 726 | // For example, X - (X + 1) -> -1 |
| 727 | X = Op0; |
| 728 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 729 | // See if "V === X - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 730 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 731 | // It does! Now see if "V - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 732 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 733 | // It does, we successfully reassociated! |
| 734 | ++NumReassoc; |
| 735 | return W; |
| 736 | } |
| 737 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 738 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 739 | // It does! Now see if "V - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 740 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 741 | // It does, we successfully reassociated! |
| 742 | ++NumReassoc; |
| 743 | return W; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 748 | // For example, X - (X - Y) -> Y. |
| 749 | Z = Op0; |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 750 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 751 | // See if "V === Z - X" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 752 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 753 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 754 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 755 | // It does, we successfully reassociated! |
| 756 | ++NumReassoc; |
| 757 | return W; |
| 758 | } |
| 759 | |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 760 | // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. |
| 761 | if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) && |
| 762 | match(Op1, m_Trunc(m_Value(Y)))) |
| 763 | if (X->getType() == Y->getType()) |
| 764 | // See if "V === X - Y" simplifies. |
| 765 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
| 766 | // It does! Now see if "trunc V" simplifies. |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 767 | if (Value *W = SimplifyCastInst(Instruction::Trunc, V, Op0->getType(), |
| 768 | Q, MaxRecurse - 1)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 769 | // It does, return the simplified "trunc V". |
| 770 | return W; |
| 771 | |
| 772 | // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 773 | if (match(Op0, m_PtrToInt(m_Value(X))) && |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 774 | match(Op1, m_PtrToInt(m_Value(Y)))) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 775 | if (Constant *Result = computePointerDifference(Q.DL, X, Y)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 776 | return ConstantExpr::getIntegerCast(Result, Op0->getType(), true); |
| 777 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 778 | // i1 sub -> xor. |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 779 | if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 780 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 781 | return V; |
| 782 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 783 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 784 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 785 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 786 | // only if B and C are equal. If B and C are equal then (since we assume |
| 787 | // that operands have already been simplified) "select(cond, B, C)" should |
| 788 | // have been simplified to the common value of B and C already. Analysing |
| 789 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 790 | // for threading over phi nodes. |
| 791 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 792 | return nullptr; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 795 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 796 | const SimplifyQuery &Q) { |
| 797 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit); |
| 798 | } |
| 799 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 800 | /// Given operands for a Mul, see if we can fold the result. |
| 801 | /// If not, this returns null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 802 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 803 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 804 | if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q)) |
| 805 | return C; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 806 | |
| 807 | // X * undef -> 0 |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 808 | // X * 0 -> 0 |
Sanjay Patel | 30be665 | 2018-04-22 17:07:44 +0000 | [diff] [blame] | 809 | if (match(Op1, m_CombineOr(m_Undef(), m_Zero()))) |
| 810 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 811 | |
| 812 | // X * 1 -> X |
| 813 | if (match(Op1, m_One())) |
| 814 | return Op0; |
| 815 | |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 816 | // (X / Y) * Y -> X if the division is exact. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 817 | Value *X = nullptr; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 818 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 819 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 820 | return X; |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 821 | |
Nick Lewycky | b89d9a4 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 822 | // i1 mul -> and. |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 823 | if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 824 | if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 825 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 826 | |
| 827 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 828 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 829 | MaxRecurse)) |
| 830 | return V; |
| 831 | |
Dmitry Venikov | d2257be | 2018-01-02 05:47:42 +0000 | [diff] [blame] | 832 | // Mul distributes over Add. Try some generic simplifications based on this. |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 833 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 834 | Q, MaxRecurse)) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 835 | return V; |
| 836 | |
| 837 | // If the operation is with the result of a select instruction, check whether |
| 838 | // operating on either branch of the select always yields the same value. |
| 839 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 840 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 841 | MaxRecurse)) |
| 842 | return V; |
| 843 | |
| 844 | // If the operation is with the result of a phi instruction, check whether |
| 845 | // operating on all incoming values of the phi always yields the same value. |
| 846 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 847 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 848 | MaxRecurse)) |
| 849 | return V; |
| 850 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 851 | return nullptr; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 854 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
| 855 | return ::SimplifyMulInst(Op0, Op1, Q, RecursionLimit); |
| 856 | } |
| 857 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 858 | /// Check for common or similar folds of integer division or integer remainder. |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 859 | /// This applies to all 4 opcodes (sdiv/udiv/srem/urem). |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 860 | static Value *simplifyDivRem(Value *Op0, Value *Op1, bool IsDiv) { |
| 861 | Type *Ty = Op0->getType(); |
| 862 | |
| 863 | // X / undef -> undef |
| 864 | // X % undef -> undef |
| 865 | if (match(Op1, m_Undef())) |
| 866 | return Op1; |
| 867 | |
| 868 | // X / 0 -> undef |
| 869 | // X % 0 -> undef |
| 870 | // We don't need to preserve faults! |
| 871 | if (match(Op1, m_Zero())) |
| 872 | return UndefValue::get(Ty); |
| 873 | |
Zvi Rackover | 51f0d64 | 2018-01-24 17:22:00 +0000 | [diff] [blame] | 874 | // If any element of a constant divisor vector is zero or undef, the whole op |
| 875 | // is undef. |
Sanjay Patel | 2b1f6f4 | 2017-03-09 16:20:52 +0000 | [diff] [blame] | 876 | auto *Op1C = dyn_cast<Constant>(Op1); |
| 877 | if (Op1C && Ty->isVectorTy()) { |
| 878 | unsigned NumElts = Ty->getVectorNumElements(); |
| 879 | for (unsigned i = 0; i != NumElts; ++i) { |
| 880 | Constant *Elt = Op1C->getAggregateElement(i); |
Zvi Rackover | 51f0d64 | 2018-01-24 17:22:00 +0000 | [diff] [blame] | 881 | if (Elt && (Elt->isNullValue() || isa<UndefValue>(Elt))) |
Sanjay Patel | 2b1f6f4 | 2017-03-09 16:20:52 +0000 | [diff] [blame] | 882 | return UndefValue::get(Ty); |
| 883 | } |
| 884 | } |
| 885 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 886 | // undef / X -> 0 |
| 887 | // undef % X -> 0 |
| 888 | if (match(Op0, m_Undef())) |
| 889 | return Constant::getNullValue(Ty); |
| 890 | |
| 891 | // 0 / X -> 0 |
| 892 | // 0 % X -> 0 |
| 893 | if (match(Op0, m_Zero())) |
Sanjay Patel | 30be665 | 2018-04-22 17:07:44 +0000 | [diff] [blame] | 894 | return Constant::getNullValue(Op0->getType()); |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 895 | |
| 896 | // X / X -> 1 |
| 897 | // X % X -> 0 |
| 898 | if (Op0 == Op1) |
| 899 | return IsDiv ? ConstantInt::get(Ty, 1) : Constant::getNullValue(Ty); |
| 900 | |
| 901 | // X / 1 -> X |
| 902 | // X % 1 -> 0 |
Sanjay Patel | 962a843 | 2017-03-09 21:56:03 +0000 | [diff] [blame] | 903 | // If this is a boolean op (single-bit element type), we can't have |
| 904 | // division-by-zero or remainder-by-zero, so assume the divisor is 1. |
Sanjay Patel | 1e911fa | 2018-06-25 18:51:21 +0000 | [diff] [blame] | 905 | // Similarly, if we're zero-extending a boolean divisor, then assume it's a 1. |
| 906 | Value *X; |
| 907 | if (match(Op1, m_One()) || Ty->isIntOrIntVectorTy(1) || |
| 908 | (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))) |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 909 | return IsDiv ? Op0 : Constant::getNullValue(Ty); |
| 910 | |
| 911 | return nullptr; |
| 912 | } |
| 913 | |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 914 | /// Given a predicate and two operands, return true if the comparison is true. |
| 915 | /// This is a helper for div/rem simplification where we return some other value |
| 916 | /// when we can prove a relationship between the operands. |
| 917 | static bool isICmpTrue(ICmpInst::Predicate Pred, Value *LHS, Value *RHS, |
| 918 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
| 919 | Value *V = SimplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse); |
| 920 | Constant *C = dyn_cast_or_null<Constant>(V); |
| 921 | return (C && C->isAllOnesValue()); |
| 922 | } |
| 923 | |
| 924 | /// Return true if we can simplify X / Y to 0. Remainder can adapt that answer |
| 925 | /// to simplify X % Y to X. |
Sanjay Patel | 0d4fd5b | 2017-09-14 14:59:07 +0000 | [diff] [blame] | 926 | static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q, |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 927 | unsigned MaxRecurse, bool IsSigned) { |
| 928 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 929 | if (!MaxRecurse--) |
| 930 | return false; |
| 931 | |
| 932 | if (IsSigned) { |
Sanjay Patel | 0d4fd5b | 2017-09-14 14:59:07 +0000 | [diff] [blame] | 933 | // |X| / |Y| --> 0 |
| 934 | // |
| 935 | // We require that 1 operand is a simple constant. That could be extended to |
| 936 | // 2 variables if we computed the sign bit for each. |
| 937 | // |
| 938 | // Make sure that a constant is not the minimum signed value because taking |
| 939 | // the abs() of that is undefined. |
| 940 | Type *Ty = X->getType(); |
| 941 | const APInt *C; |
| 942 | if (match(X, m_APInt(C)) && !C->isMinSignedValue()) { |
| 943 | // Is the variable divisor magnitude always greater than the constant |
| 944 | // dividend magnitude? |
| 945 | // |Y| > |C| --> Y < -abs(C) or Y > abs(C) |
| 946 | Constant *PosDividendC = ConstantInt::get(Ty, C->abs()); |
| 947 | Constant *NegDividendC = ConstantInt::get(Ty, -C->abs()); |
| 948 | if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) || |
| 949 | isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse)) |
| 950 | return true; |
| 951 | } |
| 952 | if (match(Y, m_APInt(C))) { |
| 953 | // Special-case: we can't take the abs() of a minimum signed value. If |
| 954 | // that's the divisor, then all we have to do is prove that the dividend |
| 955 | // is also not the minimum signed value. |
| 956 | if (C->isMinSignedValue()) |
| 957 | return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse); |
| 958 | |
| 959 | // Is the variable dividend magnitude always less than the constant |
| 960 | // divisor magnitude? |
| 961 | // |X| < |C| --> X > -abs(C) and X < abs(C) |
| 962 | Constant *PosDivisorC = ConstantInt::get(Ty, C->abs()); |
| 963 | Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs()); |
| 964 | if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) && |
| 965 | isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse)) |
| 966 | return true; |
| 967 | } |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 968 | return false; |
| 969 | } |
| 970 | |
| 971 | // IsSigned == false. |
Sanjay Patel | 0d4fd5b | 2017-09-14 14:59:07 +0000 | [diff] [blame] | 972 | // Is the dividend unsigned less than the divisor? |
| 973 | return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse); |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 976 | /// These are simplifications common to SDiv and UDiv. |
| 977 | static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 978 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 979 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
| 980 | return C; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 981 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 982 | if (Value *V = simplifyDivRem(Op0, Op1, true)) |
| 983 | return V; |
| 984 | |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 985 | bool IsSigned = Opcode == Instruction::SDiv; |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 986 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 987 | // (X * Y) / Y -> X if the multiplication does not overflow. |
Sanjay Patel | 33cb845 | 2018-01-19 16:12:55 +0000 | [diff] [blame] | 988 | Value *X; |
| 989 | if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) { |
| 990 | auto *Mul = cast<OverflowingBinaryOperator>(Op0); |
| 991 | // If the Mul does not overflow, then we are good to go. |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 992 | if ((IsSigned && Mul->hasNoSignedWrap()) || |
| 993 | (!IsSigned && Mul->hasNoUnsignedWrap())) |
Duncan Sands | 5747aba | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 994 | return X; |
Sanjay Patel | 33cb845 | 2018-01-19 16:12:55 +0000 | [diff] [blame] | 995 | // If X has the form X = A / Y, then X * Y cannot overflow. |
| 996 | if ((IsSigned && match(X, m_SDiv(m_Value(), m_Specific(Op1)))) || |
| 997 | (!IsSigned && match(X, m_UDiv(m_Value(), m_Specific(Op1))))) |
| 998 | return X; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1001 | // (X rem Y) / Y -> 0 |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 1002 | if ((IsSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1003 | (!IsSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1004 | return Constant::getNullValue(Op0->getType()); |
| 1005 | |
David Majnemer | cb9d596 | 2014-10-11 10:20:01 +0000 | [diff] [blame] | 1006 | // (X /u C1) /u C2 -> 0 if C1 * C2 overflow |
| 1007 | ConstantInt *C1, *C2; |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 1008 | if (!IsSigned && match(Op0, m_UDiv(m_Value(X), m_ConstantInt(C1))) && |
David Majnemer | cb9d596 | 2014-10-11 10:20:01 +0000 | [diff] [blame] | 1009 | match(Op1, m_ConstantInt(C2))) { |
| 1010 | bool Overflow; |
Craig Topper | 9b71a40 | 2017-04-19 21:09:45 +0000 | [diff] [blame] | 1011 | (void)C1->getValue().umul_ov(C2->getValue(), Overflow); |
David Majnemer | cb9d596 | 2014-10-11 10:20:01 +0000 | [diff] [blame] | 1012 | if (Overflow) |
| 1013 | return Constant::getNullValue(Op0->getType()); |
| 1014 | } |
| 1015 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1016 | // If the operation is with the result of a select instruction, check whether |
| 1017 | // operating on either branch of the select always yields the same value. |
| 1018 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1019 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1020 | return V; |
| 1021 | |
| 1022 | // If the operation is with the result of a phi instruction, check whether |
| 1023 | // operating on all incoming values of the phi always yields the same value. |
| 1024 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1025 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1026 | return V; |
| 1027 | |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 1028 | if (isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned)) |
| 1029 | return Constant::getNullValue(Op0->getType()); |
| 1030 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1031 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 1034 | /// These are simplifications common to SRem and URem. |
| 1035 | static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1036 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1037 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
| 1038 | return C; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1039 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1040 | if (Value *V = simplifyDivRem(Op0, Op1, false)) |
| 1041 | return V; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1042 | |
David Majnemer | b435a42 | 2014-09-17 04:16:35 +0000 | [diff] [blame] | 1043 | // (X % Y) % Y -> X % Y |
| 1044 | if ((Opcode == Instruction::SRem && |
| 1045 | match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1046 | (Opcode == Instruction::URem && |
| 1047 | match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1048 | return Op0; |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1049 | |
Anton Bikineev | 82f6115 | 2018-01-23 09:27:47 +0000 | [diff] [blame] | 1050 | // (X << Y) % X -> 0 |
| 1051 | if ((Opcode == Instruction::SRem && |
| 1052 | match(Op0, m_NSWShl(m_Specific(Op1), m_Value()))) || |
| 1053 | (Opcode == Instruction::URem && |
| 1054 | match(Op0, m_NUWShl(m_Specific(Op1), m_Value())))) |
| 1055 | return Constant::getNullValue(Op0->getType()); |
| 1056 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1057 | // If the operation is with the result of a select instruction, check whether |
| 1058 | // operating on either branch of the select always yields the same value. |
| 1059 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1060 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1061 | return V; |
| 1062 | |
| 1063 | // If the operation is with the result of a phi instruction, check whether |
| 1064 | // operating on all incoming values of the phi always yields the same value. |
| 1065 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1066 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1067 | return V; |
| 1068 | |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 1069 | // If X / Y == 0, then X % Y == X. |
| 1070 | if (isDivZero(Op0, Op1, Q, MaxRecurse, Opcode == Instruction::SRem)) |
| 1071 | return Op0; |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 1072 | |
| 1073 | return nullptr; |
| 1074 | } |
| 1075 | |
| 1076 | /// Given operands for an SDiv, see if we can fold the result. |
| 1077 | /// If not, this returns null. |
| 1078 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
| 1079 | unsigned MaxRecurse) { |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 1080 | return simplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse); |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
| 1084 | return ::SimplifySDivInst(Op0, Op1, Q, RecursionLimit); |
| 1085 | } |
| 1086 | |
| 1087 | /// Given operands for a UDiv, see if we can fold the result. |
| 1088 | /// If not, this returns null. |
| 1089 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
| 1090 | unsigned MaxRecurse) { |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 1091 | return simplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse); |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
| 1095 | return ::SimplifyUDivInst(Op0, Op1, Q, RecursionLimit); |
| 1096 | } |
| 1097 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1098 | /// Given operands for an SRem, see if we can fold the result. |
| 1099 | /// If not, this returns null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1100 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1101 | unsigned MaxRecurse) { |
Sanjay Patel | 2b7e310 | 2018-06-26 15:32:54 +0000 | [diff] [blame] | 1102 | // If the divisor is 0, the result is undefined, so assume the divisor is -1. |
| 1103 | // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0 |
| 1104 | Value *X; |
| 1105 | if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) |
| 1106 | return ConstantInt::getNullValue(Op0->getType()); |
| 1107 | |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 1108 | return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1111 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
| 1112 | return ::SimplifySRemInst(Op0, Op1, Q, RecursionLimit); |
| 1113 | } |
| 1114 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1115 | /// Given operands for a URem, see if we can fold the result. |
| 1116 | /// If not, this returns null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1117 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1118 | unsigned MaxRecurse) { |
Sanjay Patel | cca8f78 | 2017-09-14 14:09:11 +0000 | [diff] [blame] | 1119 | return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1122 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
| 1123 | return ::SimplifyURemInst(Op0, Op1, Q, RecursionLimit); |
| 1124 | } |
| 1125 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1126 | /// Returns true if a shift by \c Amount always yields undef. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1127 | static bool isUndefShift(Value *Amount) { |
| 1128 | Constant *C = dyn_cast<Constant>(Amount); |
| 1129 | if (!C) |
| 1130 | return false; |
| 1131 | |
| 1132 | // X shift by undef -> undef because it may shift by the bitwidth. |
| 1133 | if (isa<UndefValue>(C)) |
| 1134 | return true; |
| 1135 | |
| 1136 | // Shifting by the bitwidth or more is undefined. |
| 1137 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1138 | if (CI->getValue().getLimitedValue() >= |
| 1139 | CI->getType()->getScalarSizeInBits()) |
| 1140 | return true; |
| 1141 | |
| 1142 | // If all lanes of a vector shift are undefined the whole shift is. |
| 1143 | if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) { |
| 1144 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; ++I) |
| 1145 | if (!isUndefShift(C->getAggregateElement(I))) |
| 1146 | return false; |
| 1147 | return true; |
| 1148 | } |
| 1149 | |
| 1150 | return false; |
| 1151 | } |
| 1152 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1153 | /// Given operands for an Shl, LShr or AShr, see if we can fold the result. |
| 1154 | /// If not, this returns null. |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1155 | static Value *SimplifyShift(Instruction::BinaryOps Opcode, Value *Op0, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1156 | Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1157 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
| 1158 | return C; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1159 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1160 | // 0 shift by X -> 0 |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1161 | if (match(Op0, m_Zero())) |
Sanjay Patel | 30be665 | 2018-04-22 17:07:44 +0000 | [diff] [blame] | 1162 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1163 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1164 | // X shift by 0 -> X |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1165 | if (match(Op1, m_Zero())) |
| 1166 | return Op0; |
| 1167 | |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1168 | // Fold undefined shifts. |
| 1169 | if (isUndefShift(Op1)) |
| 1170 | return UndefValue::get(Op0->getType()); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1171 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1172 | // If the operation is with the result of a select instruction, check whether |
| 1173 | // operating on either branch of the select always yields the same value. |
| 1174 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1175 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1176 | return V; |
| 1177 | |
| 1178 | // If the operation is with the result of a phi instruction, check whether |
| 1179 | // operating on all incoming values of the phi always yields the same value. |
| 1180 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1181 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1182 | return V; |
| 1183 | |
Sanjay Patel | 6786bc5 | 2016-05-10 20:46:54 +0000 | [diff] [blame] | 1184 | // If any bits in the shift amount make that value greater than or equal to |
| 1185 | // the number of bits in the type, the shift is undefined. |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 1186 | KnownBits Known = computeKnownBits(Op1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 1187 | if (Known.One.getLimitedValue() >= Known.getBitWidth()) |
Sanjay Patel | 6786bc5 | 2016-05-10 20:46:54 +0000 | [diff] [blame] | 1188 | return UndefValue::get(Op0->getType()); |
| 1189 | |
| 1190 | // If all valid bits in the shift amount are known zero, the first operand is |
| 1191 | // unchanged. |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 1192 | unsigned NumValidShiftBits = Log2_32_Ceil(Known.getBitWidth()); |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1193 | if (Known.countMinTrailingZeros() >= NumValidShiftBits) |
Sanjay Patel | 6786bc5 | 2016-05-10 20:46:54 +0000 | [diff] [blame] | 1194 | return Op0; |
| 1195 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1196 | return nullptr; |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1199 | /// Given operands for an Shl, LShr or AShr, see if we can |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1200 | /// fold the result. If not, this returns null. |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1201 | static Value *SimplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1202 | Value *Op1, bool isExact, const SimplifyQuery &Q, |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1203 | unsigned MaxRecurse) { |
| 1204 | if (Value *V = SimplifyShift(Opcode, Op0, Op1, Q, MaxRecurse)) |
| 1205 | return V; |
| 1206 | |
| 1207 | // X >> X -> 0 |
| 1208 | if (Op0 == Op1) |
| 1209 | return Constant::getNullValue(Op0->getType()); |
| 1210 | |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1211 | // undef >> X -> 0 |
| 1212 | // undef >> X -> undef (if it's exact) |
| 1213 | if (match(Op0, m_Undef())) |
| 1214 | return isExact ? Op0 : Constant::getNullValue(Op0->getType()); |
| 1215 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1216 | // The low bit cannot be shifted out of an exact shift if it is set. |
| 1217 | if (isExact) { |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 1218 | KnownBits Op0Known = computeKnownBits(Op0, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1219 | if (Op0Known.One[0]) |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1220 | return Op0; |
| 1221 | } |
| 1222 | |
| 1223 | return nullptr; |
| 1224 | } |
| 1225 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1226 | /// Given operands for an Shl, see if we can fold the result. |
| 1227 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1228 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1229 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1230 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1231 | return V; |
| 1232 | |
| 1233 | // undef << X -> 0 |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1234 | // undef << X -> undef if (if it's NSW/NUW) |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1235 | if (match(Op0, m_Undef())) |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1236 | return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType()); |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1238 | // (X >> A) << A -> X |
| 1239 | Value *X; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1240 | 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] | 1241 | return X; |
Roman Lebedev | 2683802 | 2018-06-07 20:03:45 +0000 | [diff] [blame] | 1242 | |
| 1243 | // shl nuw i8 C, %x -> C iff C has sign bit set. |
| 1244 | if (isNUW && match(Op0, m_Negative())) |
| 1245 | return Op0; |
| 1246 | // NOTE: could use computeKnownBits() / LazyValueInfo, |
| 1247 | // but the cost-benefit analysis suggests it isn't worth it. |
| 1248 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1249 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1252 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1253 | const SimplifyQuery &Q) { |
| 1254 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit); |
| 1255 | } |
| 1256 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1257 | /// Given operands for an LShr, see if we can fold the result. |
| 1258 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1259 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1260 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1261 | if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q, |
| 1262 | MaxRecurse)) |
| 1263 | return V; |
David Majnemer | a80fed7 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1264 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1265 | // (X << A) >> A -> X |
| 1266 | Value *X; |
David Majnemer | 4f43837 | 2014-11-04 17:38:50 +0000 | [diff] [blame] | 1267 | if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1268 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1269 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1270 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1273 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1274 | const SimplifyQuery &Q) { |
| 1275 | return ::SimplifyLShrInst(Op0, Op1, isExact, Q, RecursionLimit); |
| 1276 | } |
| 1277 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1278 | /// Given operands for an AShr, see if we can fold the result. |
| 1279 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1280 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1281 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1282 | if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q, |
| 1283 | MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1284 | return V; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1285 | |
Sanjay Patel | adf6e88 | 2018-02-18 18:05:08 +0000 | [diff] [blame] | 1286 | // all ones >>a X -> -1 |
| 1287 | // Do not return Op0 because it may contain undef elements if it's a vector. |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1288 | if (match(Op0, m_AllOnes())) |
Sanjay Patel | adf6e88 | 2018-02-18 18:05:08 +0000 | [diff] [blame] | 1289 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1291 | // (X << A) >> A -> X |
| 1292 | Value *X; |
David Majnemer | 2de97fc | 2014-11-04 17:47:13 +0000 | [diff] [blame] | 1293 | if (match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1294 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1295 | |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1296 | // Arithmetic shifting an all-sign-bit value is a no-op. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1297 | 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] | 1298 | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
| 1299 | return Op0; |
| 1300 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1301 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1304 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1305 | const SimplifyQuery &Q) { |
| 1306 | return ::SimplifyAShrInst(Op0, Op1, isExact, Q, RecursionLimit); |
| 1307 | } |
| 1308 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1309 | /// Commuted variants are assumed to be handled by calling this function again |
| 1310 | /// with the parameters swapped. |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1311 | static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, |
| 1312 | ICmpInst *UnsignedICmp, bool IsAnd) { |
| 1313 | Value *X, *Y; |
| 1314 | |
| 1315 | ICmpInst::Predicate EqPred; |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1316 | if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) || |
| 1317 | !ICmpInst::isEquality(EqPred)) |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1318 | return nullptr; |
| 1319 | |
| 1320 | ICmpInst::Predicate UnsignedPred; |
| 1321 | if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) && |
| 1322 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1323 | ; |
| 1324 | else if (match(UnsignedICmp, |
Sanjay Patel | 0c57de4 | 2018-06-20 14:22:49 +0000 | [diff] [blame] | 1325 | m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) && |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1326 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1327 | UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); |
| 1328 | else |
| 1329 | return nullptr; |
| 1330 | |
| 1331 | // X < Y && Y != 0 --> X < Y |
| 1332 | // X < Y || Y != 0 --> Y != 0 |
| 1333 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE) |
| 1334 | return IsAnd ? UnsignedICmp : ZeroICmp; |
| 1335 | |
| 1336 | // X >= Y || Y != 0 --> true |
| 1337 | // X >= Y || Y == 0 --> X >= Y |
| 1338 | if (UnsignedPred == ICmpInst::ICMP_UGE && !IsAnd) { |
| 1339 | if (EqPred == ICmpInst::ICMP_NE) |
| 1340 | return getTrue(UnsignedICmp->getType()); |
| 1341 | return UnsignedICmp; |
| 1342 | } |
| 1343 | |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1344 | // X < Y && Y == 0 --> false |
| 1345 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ && |
| 1346 | IsAnd) |
| 1347 | return getFalse(UnsignedICmp->getType()); |
| 1348 | |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1349 | return nullptr; |
| 1350 | } |
| 1351 | |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1352 | /// Commuted variants are assumed to be handled by calling this function again |
| 1353 | /// with the parameters swapped. |
| 1354 | static Value *simplifyAndOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { |
| 1355 | ICmpInst::Predicate Pred0, Pred1; |
| 1356 | Value *A ,*B; |
Sanjay Patel | 5369775 | 2016-12-06 22:09:52 +0000 | [diff] [blame] | 1357 | if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || |
| 1358 | !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1359 | return nullptr; |
| 1360 | |
| 1361 | // We have (icmp Pred0, A, B) & (icmp Pred1, A, B). |
| 1362 | // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we |
| 1363 | // can eliminate Op1 from this 'and'. |
| 1364 | if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1)) |
| 1365 | return Op0; |
| 1366 | |
| 1367 | // Check for any combination of predicates that are guaranteed to be disjoint. |
| 1368 | if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) || |
| 1369 | (Pred0 == ICmpInst::ICMP_EQ && ICmpInst::isFalseWhenEqual(Pred1)) || |
| 1370 | (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT) || |
| 1371 | (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)) |
| 1372 | return getFalse(Op0->getType()); |
| 1373 | |
| 1374 | return nullptr; |
| 1375 | } |
| 1376 | |
| 1377 | /// Commuted variants are assumed to be handled by calling this function again |
| 1378 | /// with the parameters swapped. |
Sanjay Patel | 142cb83 | 2017-05-04 18:19:17 +0000 | [diff] [blame] | 1379 | static Value *simplifyOrOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { |
| 1380 | ICmpInst::Predicate Pred0, Pred1; |
| 1381 | Value *A ,*B; |
| 1382 | if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || |
| 1383 | !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) |
| 1384 | return nullptr; |
| 1385 | |
| 1386 | // We have (icmp Pred0, A, B) | (icmp Pred1, A, B). |
| 1387 | // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we |
| 1388 | // can eliminate Op0 from this 'or'. |
| 1389 | if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1)) |
| 1390 | return Op1; |
| 1391 | |
| 1392 | // Check for any combination of predicates that cover the entire range of |
| 1393 | // possibilities. |
| 1394 | if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) || |
| 1395 | (Pred0 == ICmpInst::ICMP_NE && ICmpInst::isTrueWhenEqual(Pred1)) || |
| 1396 | (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGE) || |
| 1397 | (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGE)) |
| 1398 | return getTrue(Op0->getType()); |
| 1399 | |
| 1400 | return nullptr; |
| 1401 | } |
| 1402 | |
Sanjay Patel | 599e65b | 2017-05-07 15:11:40 +0000 | [diff] [blame] | 1403 | /// Test if a pair of compares with a shared operand and 2 constants has an |
| 1404 | /// empty set intersection, full set union, or if one compare is a superset of |
| 1405 | /// the other. |
| 1406 | static Value *simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1, |
| 1407 | bool IsAnd) { |
| 1408 | // Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)). |
| 1409 | if (Cmp0->getOperand(0) != Cmp1->getOperand(0)) |
| 1410 | return nullptr; |
| 1411 | |
| 1412 | const APInt *C0, *C1; |
| 1413 | if (!match(Cmp0->getOperand(1), m_APInt(C0)) || |
| 1414 | !match(Cmp1->getOperand(1), m_APInt(C1))) |
| 1415 | return nullptr; |
| 1416 | |
| 1417 | auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0); |
| 1418 | auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1); |
| 1419 | |
Sanjay Patel | 6745447 | 2017-05-08 16:35:02 +0000 | [diff] [blame] | 1420 | // For and-of-compares, check if the intersection is empty: |
Sanjay Patel | 599e65b | 2017-05-07 15:11:40 +0000 | [diff] [blame] | 1421 | // (icmp X, C0) && (icmp X, C1) --> empty set --> false |
| 1422 | if (IsAnd && Range0.intersectWith(Range1).isEmptySet()) |
| 1423 | return getFalse(Cmp0->getType()); |
| 1424 | |
| 1425 | // For or-of-compares, check if the union is full: |
| 1426 | // (icmp X, C0) || (icmp X, C1) --> full set --> true |
| 1427 | if (!IsAnd && Range0.unionWith(Range1).isFullSet()) |
| 1428 | return getTrue(Cmp0->getType()); |
| 1429 | |
| 1430 | // Is one range a superset of the other? |
| 1431 | // If this is and-of-compares, take the smaller set: |
| 1432 | // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42 |
| 1433 | // If this is or-of-compares, take the larger set: |
| 1434 | // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4 |
| 1435 | if (Range0.contains(Range1)) |
| 1436 | return IsAnd ? Cmp1 : Cmp0; |
| 1437 | if (Range1.contains(Range0)) |
| 1438 | return IsAnd ? Cmp0 : Cmp1; |
| 1439 | |
| 1440 | return nullptr; |
| 1441 | } |
| 1442 | |
Sanjay Patel | 6ef6aa9 | 2018-01-11 23:27:37 +0000 | [diff] [blame] | 1443 | static Value *simplifyAndOrOfICmpsWithZero(ICmpInst *Cmp0, ICmpInst *Cmp1, |
| 1444 | bool IsAnd) { |
| 1445 | ICmpInst::Predicate P0 = Cmp0->getPredicate(), P1 = Cmp1->getPredicate(); |
| 1446 | if (!match(Cmp0->getOperand(1), m_Zero()) || |
| 1447 | !match(Cmp1->getOperand(1), m_Zero()) || P0 != P1) |
| 1448 | return nullptr; |
| 1449 | |
| 1450 | if ((IsAnd && P0 != ICmpInst::ICMP_NE) || (!IsAnd && P1 != ICmpInst::ICMP_EQ)) |
| 1451 | return nullptr; |
| 1452 | |
Sanjay Patel | 4158eff | 2018-01-13 15:44:44 +0000 | [diff] [blame] | 1453 | // We have either "(X == 0 || Y == 0)" or "(X != 0 && Y != 0)". |
Sanjay Patel | 6ef6aa9 | 2018-01-11 23:27:37 +0000 | [diff] [blame] | 1454 | Value *X = Cmp0->getOperand(0); |
| 1455 | Value *Y = Cmp1->getOperand(0); |
| 1456 | |
| 1457 | // If one of the compares is a masked version of a (not) null check, then |
Sanjay Patel | 4158eff | 2018-01-13 15:44:44 +0000 | [diff] [blame] | 1458 | // that compare implies the other, so we eliminate the other. Optionally, look |
| 1459 | // through a pointer-to-int cast to match a null check of a pointer type. |
Sanjay Patel | 6ef6aa9 | 2018-01-11 23:27:37 +0000 | [diff] [blame] | 1460 | |
Sanjay Patel | 9568f42 | 2018-01-14 15:58:18 +0000 | [diff] [blame] | 1461 | // (X == 0) || (([ptrtoint] X & ?) == 0) --> ([ptrtoint] X & ?) == 0 |
| 1462 | // (X == 0) || ((? & [ptrtoint] X) == 0) --> (? & [ptrtoint] X) == 0 |
| 1463 | // (X != 0) && (([ptrtoint] X & ?) != 0) --> ([ptrtoint] X & ?) != 0 |
| 1464 | // (X != 0) && ((? & [ptrtoint] X) != 0) --> (? & [ptrtoint] X) != 0 |
Sanjay Patel | 4158eff | 2018-01-13 15:44:44 +0000 | [diff] [blame] | 1465 | if (match(Y, m_c_And(m_Specific(X), m_Value())) || |
| 1466 | match(Y, m_c_And(m_PtrToInt(m_Specific(X)), m_Value()))) |
Sanjay Patel | 6ef6aa9 | 2018-01-11 23:27:37 +0000 | [diff] [blame] | 1467 | return Cmp1; |
| 1468 | |
Sanjay Patel | 9568f42 | 2018-01-14 15:58:18 +0000 | [diff] [blame] | 1469 | // (([ptrtoint] Y & ?) == 0) || (Y == 0) --> ([ptrtoint] Y & ?) == 0 |
| 1470 | // ((? & [ptrtoint] Y) == 0) || (Y == 0) --> (? & [ptrtoint] Y) == 0 |
| 1471 | // (([ptrtoint] Y & ?) != 0) && (Y != 0) --> ([ptrtoint] Y & ?) != 0 |
| 1472 | // ((? & [ptrtoint] Y) != 0) && (Y != 0) --> (? & [ptrtoint] Y) != 0 |
Sanjay Patel | 4158eff | 2018-01-13 15:44:44 +0000 | [diff] [blame] | 1473 | if (match(X, m_c_And(m_Specific(Y), m_Value())) || |
| 1474 | match(X, m_c_And(m_PtrToInt(m_Specific(Y)), m_Value()))) |
Sanjay Patel | 6ef6aa9 | 2018-01-11 23:27:37 +0000 | [diff] [blame] | 1475 | return Cmp0; |
| 1476 | |
| 1477 | return nullptr; |
| 1478 | } |
| 1479 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1480 | static Value *simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1) { |
Sanjay Patel | 599e65b | 2017-05-07 15:11:40 +0000 | [diff] [blame] | 1481 | // (icmp (add V, C0), C1) & (icmp V, C0) |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1482 | ICmpInst::Predicate Pred0, Pred1; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1483 | const APInt *C0, *C1; |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1484 | Value *V; |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1485 | 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] | 1486 | return nullptr; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1487 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1488 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1489 | return nullptr; |
| 1490 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1491 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1492 | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
| 1493 | return nullptr; |
| 1494 | |
Craig Topper | 9bce1ad | 2017-05-26 19:04:02 +0000 | [diff] [blame] | 1495 | Type *ITy = Op0->getType(); |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1496 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1497 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1498 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1499 | const APInt Delta = *C1 - *C0; |
| 1500 | if (C0->isStrictlyPositive()) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1501 | if (Delta == 2) { |
| 1502 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT) |
| 1503 | return getFalse(ITy); |
| 1504 | if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1505 | return getFalse(ITy); |
| 1506 | } |
| 1507 | if (Delta == 1) { |
| 1508 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT) |
| 1509 | return getFalse(ITy); |
| 1510 | if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1511 | return getFalse(ITy); |
| 1512 | } |
| 1513 | } |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1514 | if (C0->getBoolValue() && isNUW) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1515 | if (Delta == 2) |
| 1516 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT) |
| 1517 | return getFalse(ITy); |
| 1518 | if (Delta == 1) |
| 1519 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT) |
| 1520 | return getFalse(ITy); |
| 1521 | } |
| 1522 | |
| 1523 | return nullptr; |
| 1524 | } |
| 1525 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1526 | static Value *simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
| 1527 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true)) |
| 1528 | return X; |
| 1529 | if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/true)) |
Sanjay Patel | 142cb83 | 2017-05-04 18:19:17 +0000 | [diff] [blame] | 1530 | return X; |
| 1531 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1532 | if (Value *X = simplifyAndOfICmpsWithSameOperands(Op0, Op1)) |
| 1533 | return X; |
| 1534 | if (Value *X = simplifyAndOfICmpsWithSameOperands(Op1, Op0)) |
Sanjay Patel | 142cb83 | 2017-05-04 18:19:17 +0000 | [diff] [blame] | 1535 | return X; |
| 1536 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1537 | if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, true)) |
Sanjay Patel | 599e65b | 2017-05-07 15:11:40 +0000 | [diff] [blame] | 1538 | return X; |
| 1539 | |
Sanjay Patel | 6ef6aa9 | 2018-01-11 23:27:37 +0000 | [diff] [blame] | 1540 | if (Value *X = simplifyAndOrOfICmpsWithZero(Op0, Op1, true)) |
| 1541 | return X; |
| 1542 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1543 | if (Value *X = simplifyAndOfICmpsWithAdd(Op0, Op1)) |
| 1544 | return X; |
| 1545 | if (Value *X = simplifyAndOfICmpsWithAdd(Op1, Op0)) |
| 1546 | return X; |
| 1547 | |
| 1548 | return nullptr; |
| 1549 | } |
| 1550 | |
| 1551 | static Value *simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1) { |
Sanjay Patel | 142cb83 | 2017-05-04 18:19:17 +0000 | [diff] [blame] | 1552 | // (icmp (add V, C0), C1) | (icmp V, C0) |
| 1553 | ICmpInst::Predicate Pred0, Pred1; |
| 1554 | const APInt *C0, *C1; |
| 1555 | Value *V; |
| 1556 | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1)))) |
| 1557 | return nullptr; |
| 1558 | |
| 1559 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
| 1560 | return nullptr; |
| 1561 | |
| 1562 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
| 1563 | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
| 1564 | return nullptr; |
| 1565 | |
| 1566 | Type *ITy = Op0->getType(); |
| 1567 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1568 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1569 | |
| 1570 | const APInt Delta = *C1 - *C0; |
| 1571 | if (C0->isStrictlyPositive()) { |
| 1572 | if (Delta == 2) { |
| 1573 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE) |
| 1574 | return getTrue(ITy); |
| 1575 | if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1576 | return getTrue(ITy); |
| 1577 | } |
| 1578 | if (Delta == 1) { |
| 1579 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE) |
| 1580 | return getTrue(ITy); |
| 1581 | if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1582 | return getTrue(ITy); |
| 1583 | } |
| 1584 | } |
| 1585 | if (C0->getBoolValue() && isNUW) { |
| 1586 | if (Delta == 2) |
| 1587 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE) |
| 1588 | return getTrue(ITy); |
| 1589 | if (Delta == 1) |
| 1590 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE) |
| 1591 | return getTrue(ITy); |
| 1592 | } |
| 1593 | |
| 1594 | return nullptr; |
| 1595 | } |
| 1596 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1597 | static Value *simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
| 1598 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false)) |
| 1599 | return X; |
| 1600 | if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/false)) |
| 1601 | return X; |
Sanjay Patel | e42b4d5 | 2017-05-04 19:51:34 +0000 | [diff] [blame] | 1602 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1603 | if (Value *X = simplifyOrOfICmpsWithSameOperands(Op0, Op1)) |
| 1604 | return X; |
| 1605 | if (Value *X = simplifyOrOfICmpsWithSameOperands(Op1, Op0)) |
| 1606 | return X; |
| 1607 | |
| 1608 | if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, false)) |
| 1609 | return X; |
| 1610 | |
Sanjay Patel | 6ef6aa9 | 2018-01-11 23:27:37 +0000 | [diff] [blame] | 1611 | if (Value *X = simplifyAndOrOfICmpsWithZero(Op0, Op1, false)) |
| 1612 | return X; |
| 1613 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1614 | if (Value *X = simplifyOrOfICmpsWithAdd(Op0, Op1)) |
| 1615 | return X; |
| 1616 | if (Value *X = simplifyOrOfICmpsWithAdd(Op1, Op0)) |
| 1617 | return X; |
Sanjay Patel | e42b4d5 | 2017-05-04 19:51:34 +0000 | [diff] [blame] | 1618 | |
| 1619 | return nullptr; |
| 1620 | } |
| 1621 | |
Sanjay Patel | eb731b0 | 2017-11-19 15:34:27 +0000 | [diff] [blame] | 1622 | static Value *simplifyAndOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd) { |
| 1623 | Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1); |
| 1624 | Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1); |
| 1625 | if (LHS0->getType() != RHS0->getType()) |
| 1626 | return nullptr; |
| 1627 | |
| 1628 | FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
| 1629 | if ((PredL == FCmpInst::FCMP_ORD && PredR == FCmpInst::FCMP_ORD && IsAnd) || |
| 1630 | (PredL == FCmpInst::FCMP_UNO && PredR == FCmpInst::FCMP_UNO && !IsAnd)) { |
| 1631 | // (fcmp ord NNAN, X) & (fcmp ord X, Y) --> fcmp ord X, Y |
| 1632 | // (fcmp ord NNAN, X) & (fcmp ord Y, X) --> fcmp ord Y, X |
| 1633 | // (fcmp ord X, NNAN) & (fcmp ord X, Y) --> fcmp ord X, Y |
| 1634 | // (fcmp ord X, NNAN) & (fcmp ord Y, X) --> fcmp ord Y, X |
| 1635 | // (fcmp uno NNAN, X) | (fcmp uno X, Y) --> fcmp uno X, Y |
| 1636 | // (fcmp uno NNAN, X) | (fcmp uno Y, X) --> fcmp uno Y, X |
| 1637 | // (fcmp uno X, NNAN) | (fcmp uno X, Y) --> fcmp uno X, Y |
| 1638 | // (fcmp uno X, NNAN) | (fcmp uno Y, X) --> fcmp uno Y, X |
| 1639 | if ((isKnownNeverNaN(LHS0) && (LHS1 == RHS0 || LHS1 == RHS1)) || |
| 1640 | (isKnownNeverNaN(LHS1) && (LHS0 == RHS0 || LHS0 == RHS1))) |
| 1641 | return RHS; |
| 1642 | |
| 1643 | // (fcmp ord X, Y) & (fcmp ord NNAN, X) --> fcmp ord X, Y |
| 1644 | // (fcmp ord Y, X) & (fcmp ord NNAN, X) --> fcmp ord Y, X |
| 1645 | // (fcmp ord X, Y) & (fcmp ord X, NNAN) --> fcmp ord X, Y |
| 1646 | // (fcmp ord Y, X) & (fcmp ord X, NNAN) --> fcmp ord Y, X |
| 1647 | // (fcmp uno X, Y) | (fcmp uno NNAN, X) --> fcmp uno X, Y |
| 1648 | // (fcmp uno Y, X) | (fcmp uno NNAN, X) --> fcmp uno Y, X |
| 1649 | // (fcmp uno X, Y) | (fcmp uno X, NNAN) --> fcmp uno X, Y |
| 1650 | // (fcmp uno Y, X) | (fcmp uno X, NNAN) --> fcmp uno Y, X |
| 1651 | if ((isKnownNeverNaN(RHS0) && (RHS1 == LHS0 || RHS1 == LHS1)) || |
| 1652 | (isKnownNeverNaN(RHS1) && (RHS0 == LHS0 || RHS0 == LHS1))) |
| 1653 | return LHS; |
| 1654 | } |
| 1655 | |
| 1656 | return nullptr; |
| 1657 | } |
| 1658 | |
| 1659 | static Value *simplifyAndOrOfCmps(Value *Op0, Value *Op1, bool IsAnd) { |
Sanjay Patel | e42b4d5 | 2017-05-04 19:51:34 +0000 | [diff] [blame] | 1660 | // Look through casts of the 'and' operands to find compares. |
| 1661 | auto *Cast0 = dyn_cast<CastInst>(Op0); |
| 1662 | auto *Cast1 = dyn_cast<CastInst>(Op1); |
| 1663 | if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() && |
| 1664 | Cast0->getSrcTy() == Cast1->getSrcTy()) { |
| 1665 | Op0 = Cast0->getOperand(0); |
| 1666 | Op1 = Cast1->getOperand(0); |
| 1667 | } |
| 1668 | |
Sanjay Patel | eb731b0 | 2017-11-19 15:34:27 +0000 | [diff] [blame] | 1669 | Value *V = nullptr; |
| 1670 | auto *ICmp0 = dyn_cast<ICmpInst>(Op0); |
| 1671 | auto *ICmp1 = dyn_cast<ICmpInst>(Op1); |
| 1672 | if (ICmp0 && ICmp1) |
| 1673 | V = IsAnd ? simplifyAndOfICmps(ICmp0, ICmp1) : |
| 1674 | simplifyOrOfICmps(ICmp0, ICmp1); |
Sanjay Patel | e42b4d5 | 2017-05-04 19:51:34 +0000 | [diff] [blame] | 1675 | |
Sanjay Patel | eb731b0 | 2017-11-19 15:34:27 +0000 | [diff] [blame] | 1676 | auto *FCmp0 = dyn_cast<FCmpInst>(Op0); |
| 1677 | auto *FCmp1 = dyn_cast<FCmpInst>(Op1); |
| 1678 | if (FCmp0 && FCmp1) |
| 1679 | V = simplifyAndOrOfFCmps(FCmp0, FCmp1, IsAnd); |
| 1680 | |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1681 | if (!V) |
| 1682 | return nullptr; |
| 1683 | if (!Cast0) |
Sanjay Patel | e42b4d5 | 2017-05-04 19:51:34 +0000 | [diff] [blame] | 1684 | return V; |
Craig Topper | 348314d | 2017-05-26 22:42:34 +0000 | [diff] [blame] | 1685 | |
| 1686 | // If we looked through casts, we can only handle a constant simplification |
| 1687 | // because we are not allowed to create a cast instruction here. |
| 1688 | if (auto *C = dyn_cast<Constant>(V)) |
| 1689 | return ConstantExpr::getCast(Cast0->getOpcode(), C, Cast0->getType()); |
Sanjay Patel | e42b4d5 | 2017-05-04 19:51:34 +0000 | [diff] [blame] | 1690 | |
| 1691 | return nullptr; |
| 1692 | } |
| 1693 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1694 | /// Given operands for an And, see if we can fold the result. |
| 1695 | /// If not, this returns null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1696 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1697 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1698 | if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q)) |
| 1699 | return C; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1701 | // X & undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1702 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1703 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1704 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1705 | // X & X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1706 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1707 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1708 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1709 | // X & 0 = 0 |
| 1710 | if (match(Op1, m_Zero())) |
Sanjay Patel | 30be665 | 2018-04-22 17:07:44 +0000 | [diff] [blame] | 1711 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1712 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1713 | // X & -1 = X |
| 1714 | if (match(Op1, m_AllOnes())) |
| 1715 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1717 | // A & ~A = ~A & A = 0 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1718 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1719 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1720 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1721 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1722 | // (A | ?) & A = A |
Craig Topper | dad7d8d | 2017-07-16 06:57:41 +0000 | [diff] [blame] | 1723 | if (match(Op0, m_c_Or(m_Specific(Op1), m_Value()))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1724 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1725 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1726 | // A & (A | ?) = A |
Craig Topper | dad7d8d | 2017-07-16 06:57:41 +0000 | [diff] [blame] | 1727 | if (match(Op1, m_c_Or(m_Specific(Op0), m_Value()))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1728 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1729 | |
Sanjay Patel | 877364f | 2017-05-16 21:51:04 +0000 | [diff] [blame] | 1730 | // A mask that only clears known zeros of a shifted value is a no-op. |
| 1731 | Value *X; |
| 1732 | const APInt *Mask; |
| 1733 | const APInt *ShAmt; |
| 1734 | if (match(Op1, m_APInt(Mask))) { |
| 1735 | // If all bits in the inverted and shifted mask are clear: |
| 1736 | // and (shl X, ShAmt), Mask --> shl X, ShAmt |
| 1737 | if (match(Op0, m_Shl(m_Value(X), m_APInt(ShAmt))) && |
| 1738 | (~(*Mask)).lshr(*ShAmt).isNullValue()) |
| 1739 | return Op0; |
| 1740 | |
| 1741 | // If all bits in the inverted and shifted mask are clear: |
| 1742 | // and (lshr X, ShAmt), Mask --> lshr X, ShAmt |
| 1743 | if (match(Op0, m_LShr(m_Value(X), m_APInt(ShAmt))) && |
| 1744 | (~(*Mask)).shl(*ShAmt).isNullValue()) |
| 1745 | return Op0; |
| 1746 | } |
| 1747 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1748 | // A & (-A) = A if A is a power of two or zero. |
| 1749 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1750 | match(Op1, m_Neg(m_Specific(Op0)))) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1751 | if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1752 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1753 | return Op0; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1754 | if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1755 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1756 | return Op1; |
| 1757 | } |
| 1758 | |
Sanjay Patel | eb731b0 | 2017-11-19 15:34:27 +0000 | [diff] [blame] | 1759 | if (Value *V = simplifyAndOrOfCmps(Op0, Op1, true)) |
Sanjay Patel | e42b4d5 | 2017-05-04 19:51:34 +0000 | [diff] [blame] | 1760 | return V; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1761 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1762 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1763 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, |
| 1764 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1765 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1766 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1767 | // And distributes over Or. Try some generic simplifications based on this. |
| 1768 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1769 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1770 | return V; |
| 1771 | |
| 1772 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1773 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1774 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1775 | return V; |
| 1776 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1777 | // If the operation is with the result of a select instruction, check whether |
| 1778 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1779 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1780 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, |
| 1781 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1782 | return V; |
| 1783 | |
| 1784 | // If the operation is with the result of a phi instruction, check whether |
| 1785 | // 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] | 1786 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1787 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1788 | MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1789 | return V; |
| 1790 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1791 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1794 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
| 1795 | return ::SimplifyAndInst(Op0, Op1, Q, RecursionLimit); |
| 1796 | } |
| 1797 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1798 | /// Given operands for an Or, see if we can fold the result. |
| 1799 | /// If not, this returns null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1800 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1801 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1802 | if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q)) |
| 1803 | return C; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1804 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1805 | // X | undef -> -1 |
Sanjay Patel | adf6e88 | 2018-02-18 18:05:08 +0000 | [diff] [blame] | 1806 | // X | -1 = -1 |
| 1807 | // Do not return Op1 because it may contain undef elements if it's a vector. |
| 1808 | if (match(Op1, m_Undef()) || match(Op1, m_AllOnes())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1809 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1810 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1811 | // X | X = X |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1812 | // X | 0 = X |
Sanjay Patel | adf6e88 | 2018-02-18 18:05:08 +0000 | [diff] [blame] | 1813 | if (Op0 == Op1 || match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1814 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1815 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1816 | // A | ~A = ~A | A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1817 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1818 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1819 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1820 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1821 | // (A & ?) | A = A |
Craig Topper | dad7d8d | 2017-07-16 06:57:41 +0000 | [diff] [blame] | 1822 | if (match(Op0, m_c_And(m_Specific(Op1), m_Value()))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1823 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1825 | // A | (A & ?) = A |
Craig Topper | dad7d8d | 2017-07-16 06:57:41 +0000 | [diff] [blame] | 1826 | if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1827 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1828 | |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1829 | // ~(A & ?) | A = -1 |
Craig Topper | dad7d8d | 2017-07-16 06:57:41 +0000 | [diff] [blame] | 1830 | if (match(Op0, m_Not(m_c_And(m_Specific(Op1), m_Value())))) |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1831 | return Constant::getAllOnesValue(Op1->getType()); |
| 1832 | |
| 1833 | // A | ~(A & ?) = -1 |
Craig Topper | dad7d8d | 2017-07-16 06:57:41 +0000 | [diff] [blame] | 1834 | if (match(Op1, m_Not(m_c_And(m_Specific(Op1), m_Value())))) |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1835 | return Constant::getAllOnesValue(Op0->getType()); |
| 1836 | |
Craig Topper | dad7d8d | 2017-07-16 06:57:41 +0000 | [diff] [blame] | 1837 | Value *A, *B; |
Sanjay Patel | 0889225 | 2017-04-24 18:24:36 +0000 | [diff] [blame] | 1838 | // (A & ~B) | (A ^ B) -> (A ^ B) |
| 1839 | // (~B & A) | (A ^ B) -> (A ^ B) |
Craig Topper | 0b650d3 | 2017-04-25 17:01:32 +0000 | [diff] [blame] | 1840 | // (A & ~B) | (B ^ A) -> (B ^ A) |
| 1841 | // (~B & A) | (B ^ A) -> (B ^ A) |
| 1842 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 1843 | (match(Op0, m_c_And(m_Specific(A), m_Not(m_Specific(B)))) || |
| 1844 | 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] | 1845 | return Op1; |
| 1846 | |
| 1847 | // Commute the 'or' operands. |
| 1848 | // (A ^ B) | (A & ~B) -> (A ^ B) |
| 1849 | // (A ^ B) | (~B & A) -> (A ^ B) |
Craig Topper | 0b650d3 | 2017-04-25 17:01:32 +0000 | [diff] [blame] | 1850 | // (B ^ A) | (A & ~B) -> (B ^ A) |
| 1851 | // (B ^ A) | (~B & A) -> (B ^ A) |
| 1852 | if (match(Op0, m_Xor(m_Value(A), m_Value(B))) && |
| 1853 | (match(Op1, m_c_And(m_Specific(A), m_Not(m_Specific(B)))) || |
| 1854 | 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] | 1855 | return Op0; |
| 1856 | |
Craig Topper | 479daaf | 2017-05-14 07:54:43 +0000 | [diff] [blame] | 1857 | // (A & B) | (~A ^ B) -> (~A ^ B) |
| 1858 | // (B & A) | (~A ^ B) -> (~A ^ B) |
| 1859 | // (A & B) | (B ^ ~A) -> (B ^ ~A) |
| 1860 | // (B & A) | (B ^ ~A) -> (B ^ ~A) |
| 1861 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 1862 | (match(Op1, m_c_Xor(m_Specific(A), m_Not(m_Specific(B)))) || |
| 1863 | match(Op1, m_c_Xor(m_Not(m_Specific(A)), m_Specific(B))))) |
| 1864 | return Op1; |
| 1865 | |
| 1866 | // (~A ^ B) | (A & B) -> (~A ^ B) |
| 1867 | // (~A ^ B) | (B & A) -> (~A ^ B) |
| 1868 | // (B ^ ~A) | (A & B) -> (B ^ ~A) |
| 1869 | // (B ^ ~A) | (B & A) -> (B ^ ~A) |
| 1870 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
| 1871 | (match(Op0, m_c_Xor(m_Specific(A), m_Not(m_Specific(B)))) || |
| 1872 | match(Op0, m_c_Xor(m_Not(m_Specific(A)), m_Specific(B))))) |
| 1873 | return Op0; |
| 1874 | |
Sanjay Patel | eb731b0 | 2017-11-19 15:34:27 +0000 | [diff] [blame] | 1875 | if (Value *V = simplifyAndOrOfCmps(Op0, Op1, false)) |
Sanjay Patel | e42b4d5 | 2017-05-04 19:51:34 +0000 | [diff] [blame] | 1876 | return V; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1877 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1878 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1879 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, |
| 1880 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1881 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1882 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1883 | // Or distributes over And. Try some generic simplifications based on this. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1884 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q, |
| 1885 | MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1886 | return V; |
| 1887 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1888 | // If the operation is with the result of a select instruction, check whether |
| 1889 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1890 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1891 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1892 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1893 | return V; |
| 1894 | |
Craig Topper | 50500d5 | 2017-05-26 05:16:20 +0000 | [diff] [blame] | 1895 | // (A & C1)|(B & C2) |
Craig Topper | 1da22c3 | 2017-05-26 19:03:53 +0000 | [diff] [blame] | 1896 | const APInt *C1, *C2; |
| 1897 | if (match(Op0, m_And(m_Value(A), m_APInt(C1))) && |
| 1898 | match(Op1, m_And(m_Value(B), m_APInt(C2)))) { |
| 1899 | if (*C1 == ~*C2) { |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1900 | // (A & C1)|(B & C2) |
| 1901 | // If we have: ((V + N) & C1) | (V & C2) |
| 1902 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 1903 | // replace with V+N. |
Craig Topper | c8bebb1 | 2017-05-26 19:03:59 +0000 | [diff] [blame] | 1904 | Value *N; |
Craig Topper | 1da22c3 | 2017-05-26 19:03:53 +0000 | [diff] [blame] | 1905 | if (C2->isMask() && // C2 == 0+1+ |
Craig Topper | c8bebb1 | 2017-05-26 19:03:59 +0000 | [diff] [blame] | 1906 | match(A, m_c_Add(m_Specific(B), m_Value(N)))) { |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1907 | // Add commutes, try both ways. |
Craig Topper | c8bebb1 | 2017-05-26 19:03:59 +0000 | [diff] [blame] | 1908 | if (MaskedValueIsZero(N, *C2, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1909 | return A; |
| 1910 | } |
| 1911 | // Or commutes, try both ways. |
Craig Topper | 1da22c3 | 2017-05-26 19:03:53 +0000 | [diff] [blame] | 1912 | if (C1->isMask() && |
Craig Topper | c8bebb1 | 2017-05-26 19:03:59 +0000 | [diff] [blame] | 1913 | match(B, m_c_Add(m_Specific(A), m_Value(N)))) { |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1914 | // Add commutes, try both ways. |
Craig Topper | c8bebb1 | 2017-05-26 19:03:59 +0000 | [diff] [blame] | 1915 | if (MaskedValueIsZero(N, *C1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1916 | return B; |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1921 | // If the operation is with the result of a phi instruction, check whether |
| 1922 | // 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] | 1923 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1924 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1925 | return V; |
| 1926 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1927 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1930 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
| 1931 | return ::SimplifyOrInst(Op0, Op1, Q, RecursionLimit); |
| 1932 | } |
| 1933 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1934 | /// Given operands for a Xor, see if we can fold the result. |
| 1935 | /// If not, this returns null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1936 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1937 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1938 | if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q)) |
| 1939 | return C; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1940 | |
| 1941 | // A ^ undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1942 | if (match(Op1, m_Undef())) |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1943 | return Op1; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1944 | |
| 1945 | // A ^ 0 = A |
| 1946 | if (match(Op1, m_Zero())) |
| 1947 | return Op0; |
| 1948 | |
Eli Friedman | ad3cfe7 | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 1949 | // A ^ A = 0 |
| 1950 | if (Op0 == Op1) |
| 1951 | return Constant::getNullValue(Op0->getType()); |
| 1952 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1953 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1954 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1955 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1956 | return Constant::getAllOnesValue(Op0->getType()); |
| 1957 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1958 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1959 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, |
| 1960 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1961 | return V; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1962 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1963 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1964 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1965 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1966 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1967 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1968 | // have been simplified to the common value of B and C already. Analysing |
| 1969 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1970 | // for threading over phi nodes. |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1971 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1972 | return nullptr; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 1975 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
| 1976 | return ::SimplifyXorInst(Op0, Op1, Q, RecursionLimit); |
| 1977 | } |
| 1978 | |
| 1979 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1980 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1981 | return CmpInst::makeCmpResultType(Op->getType()); |
| 1982 | } |
| 1983 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1984 | /// Rummage around inside V looking for something equivalent to the comparison |
| 1985 | /// "LHS Pred RHS". Return such a value if found, otherwise return null. |
| 1986 | /// Helper function for analyzing max/min idioms. |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1987 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 1988 | Value *LHS, Value *RHS) { |
| 1989 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 1990 | if (!SI) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1991 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1992 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1993 | if (!Cmp) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1994 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1995 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 1996 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 1997 | return Cmp; |
| 1998 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 1999 | LHS == CmpRHS && RHS == CmpLHS) |
| 2000 | return Cmp; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2001 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 2004 | // A significant optimization not implemented here is assuming that alloca |
| 2005 | // addresses are not equal to incoming argument values. They don't *alias*, |
| 2006 | // as we say, but that doesn't mean they aren't equal, so we take a |
| 2007 | // conservative approach. |
| 2008 | // |
| 2009 | // This is inspired in part by C++11 5.10p1: |
| 2010 | // "Two pointers of the same type compare equal if and only if they are both |
| 2011 | // null, both point to the same function, or both represent the same |
| 2012 | // address." |
| 2013 | // |
| 2014 | // This is pretty permissive. |
| 2015 | // |
| 2016 | // It's also partly due to C11 6.5.9p6: |
| 2017 | // "Two pointers compare equal if and only if both are null pointers, both are |
| 2018 | // pointers to the same object (including a pointer to an object and a |
| 2019 | // subobject at its beginning) or function, both are pointers to one past the |
| 2020 | // last element of the same array object, or one is a pointer to one past the |
| 2021 | // 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] | 2022 | // different array object that happens to immediately follow the first array |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 2023 | // object in the address space.) |
| 2024 | // |
| 2025 | // C11's version is more restrictive, however there's no reason why an argument |
| 2026 | // couldn't be a one-past-the-end value for a stack object in the caller and be |
| 2027 | // equal to the beginning of a stack object in the callee. |
| 2028 | // |
| 2029 | // If the C and C++ standards are ever made sufficiently restrictive in this |
| 2030 | // area, it may be possible to update LLVM's semantics accordingly and reinstate |
| 2031 | // this optimization. |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2032 | static Constant * |
| 2033 | computePointerICmp(const DataLayout &DL, const TargetLibraryInfo *TLI, |
| 2034 | const DominatorTree *DT, CmpInst::Predicate Pred, |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2035 | AssumptionCache *AC, const Instruction *CxtI, |
| 2036 | Value *LHS, Value *RHS) { |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2037 | // First, skip past any trivial no-ops. |
| 2038 | LHS = LHS->stripPointerCasts(); |
| 2039 | RHS = RHS->stripPointerCasts(); |
| 2040 | |
| 2041 | // A non-null pointer is not equal to a null pointer. |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2042 | if (llvm::isKnownNonZero(LHS, DL) && isa<ConstantPointerNull>(RHS) && |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2043 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE)) |
| 2044 | return ConstantInt::get(GetCompareTy(LHS), |
| 2045 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2046 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2047 | // We can only fold certain predicates on pointer comparisons. |
| 2048 | switch (Pred) { |
| 2049 | default: |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2050 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2051 | |
| 2052 | // Equality comaprisons are easy to fold. |
| 2053 | case CmpInst::ICMP_EQ: |
| 2054 | case CmpInst::ICMP_NE: |
| 2055 | break; |
| 2056 | |
| 2057 | // We can only handle unsigned relational comparisons because 'inbounds' on |
| 2058 | // a GEP only protects against unsigned wrapping. |
| 2059 | case CmpInst::ICMP_UGT: |
| 2060 | case CmpInst::ICMP_UGE: |
| 2061 | case CmpInst::ICMP_ULT: |
| 2062 | case CmpInst::ICMP_ULE: |
| 2063 | // However, we have to switch them to their signed variants to handle |
| 2064 | // negative indices from the base pointer. |
| 2065 | Pred = ICmpInst::getSignedPredicate(Pred); |
| 2066 | break; |
| 2067 | } |
| 2068 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2069 | // Strip off any constant offsets so that we can reason about them. |
| 2070 | // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets |
| 2071 | // here and compare base addresses like AliasAnalysis does, however there are |
| 2072 | // numerous hazards. AliasAnalysis and its utilities rely on special rules |
| 2073 | // governing loads and stores which don't apply to icmps. Also, AliasAnalysis |
| 2074 | // doesn't need to guarantee pointer inequality when it says NoAlias. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2075 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 2076 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2077 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2078 | // If LHS and RHS are related via constant offsets to the same base |
| 2079 | // value, we can replace it with an icmp which just compares the offsets. |
| 2080 | if (LHS == RHS) |
| 2081 | return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2082 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2083 | // Various optimizations for (in)equality comparisons. |
| 2084 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) { |
| 2085 | // Different non-empty allocations that exist at the same time have |
| 2086 | // different addresses (if the program can tell). Global variables always |
| 2087 | // exist, so they always exist during the lifetime of each other and all |
| 2088 | // allocas. Two different allocas usually have different addresses... |
| 2089 | // |
| 2090 | // However, if there's an @llvm.stackrestore dynamically in between two |
| 2091 | // allocas, they may have the same address. It's tempting to reduce the |
| 2092 | // scope of the problem by only looking at *static* allocas here. That would |
| 2093 | // cover the majority of allocas while significantly reducing the likelihood |
| 2094 | // of having an @llvm.stackrestore pop up in the middle. However, it's not |
| 2095 | // actually impossible for an @llvm.stackrestore to pop up in the middle of |
| 2096 | // an entry block. Also, if we have a block that's not attached to a |
| 2097 | // function, we can't tell if it's "static" under the current definition. |
| 2098 | // Theoretically, this problem could be fixed by creating a new kind of |
| 2099 | // instruction kind specifically for static allocas. Such a new instruction |
| 2100 | // could be required to be at the top of the entry block, thus preventing it |
| 2101 | // from being subject to a @llvm.stackrestore. Instcombine could even |
| 2102 | // convert regular allocas into these special allocas. It'd be nifty. |
| 2103 | // However, until then, this problem remains open. |
| 2104 | // |
| 2105 | // So, we'll assume that two non-empty allocas have different addresses |
| 2106 | // for now. |
| 2107 | // |
| 2108 | // With all that, if the offsets are within the bounds of their allocations |
| 2109 | // (and not one-past-the-end! so we can't use inbounds!), and their |
| 2110 | // allocations aren't the same, the pointers are not equal. |
| 2111 | // |
| 2112 | // Note that it's not necessary to check for LHS being a global variable |
| 2113 | // address, due to canonicalization and constant folding. |
| 2114 | if (isa<AllocaInst>(LHS) && |
| 2115 | (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2116 | ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset); |
| 2117 | ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2118 | uint64_t LHSSize, RHSSize; |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2119 | if (LHSOffsetCI && RHSOffsetCI && |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2120 | getObjectSize(LHS, LHSSize, DL, TLI) && |
| 2121 | getObjectSize(RHS, RHSSize, DL, TLI)) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2122 | const APInt &LHSOffsetValue = LHSOffsetCI->getValue(); |
| 2123 | const APInt &RHSOffsetValue = RHSOffsetCI->getValue(); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2124 | if (!LHSOffsetValue.isNegative() && |
| 2125 | !RHSOffsetValue.isNegative() && |
| 2126 | LHSOffsetValue.ult(LHSSize) && |
| 2127 | RHSOffsetValue.ult(RHSSize)) { |
| 2128 | return ConstantInt::get(GetCompareTy(LHS), |
| 2129 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | // Repeat the above check but this time without depending on DataLayout |
| 2134 | // or being able to compute a precise size. |
| 2135 | if (!cast<PointerType>(LHS->getType())->isEmptyTy() && |
| 2136 | !cast<PointerType>(RHS->getType())->isEmptyTy() && |
| 2137 | LHSOffset->isNullValue() && |
| 2138 | RHSOffset->isNullValue()) |
| 2139 | return ConstantInt::get(GetCompareTy(LHS), |
| 2140 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2141 | } |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2142 | |
| 2143 | // Even if an non-inbounds GEP occurs along the path we can still optimize |
| 2144 | // equality comparisons concerning the result. We avoid walking the whole |
| 2145 | // chain again by starting where the last calls to |
| 2146 | // stripAndComputeConstantOffsets left off and accumulate the offsets. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2147 | Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true); |
| 2148 | Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true); |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2149 | if (LHS == RHS) |
| 2150 | return ConstantExpr::getICmp(Pred, |
| 2151 | ConstantExpr::getAdd(LHSOffset, LHSNoBound), |
| 2152 | ConstantExpr::getAdd(RHSOffset, RHSNoBound)); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2153 | |
| 2154 | // If one side of the equality comparison must come from a noalias call |
| 2155 | // (meaning a system memory allocation function), and the other side must |
| 2156 | // come from a pointer that cannot overlap with dynamically-allocated |
| 2157 | // memory within the lifetime of the current function (allocas, byval |
| 2158 | // arguments, globals), then determine the comparison result here. |
| 2159 | SmallVector<Value *, 8> LHSUObjs, RHSUObjs; |
| 2160 | GetUnderlyingObjects(LHS, LHSUObjs, DL); |
| 2161 | GetUnderlyingObjects(RHS, RHSUObjs, DL); |
| 2162 | |
| 2163 | // Is the set of underlying objects all noalias calls? |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2164 | auto IsNAC = [](ArrayRef<Value *> Objects) { |
| 2165 | return all_of(Objects, isNoAliasCall); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2166 | }; |
| 2167 | |
| 2168 | // 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] | 2169 | // noalias calls. For allocas, we consider only static ones (dynamic |
| 2170 | // allocas might be transformed into calls to malloc not simultaneously |
| 2171 | // live with the compared-to allocation). For globals, we exclude symbols |
| 2172 | // that might be resolve lazily to symbols in another dynamically-loaded |
| 2173 | // library (and, thus, could be malloc'ed by the implementation). |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2174 | auto IsAllocDisjoint = [](ArrayRef<Value *> Objects) { |
| 2175 | return all_of(Objects, [](Value *V) { |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2176 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) |
| 2177 | return AI->getParent() && AI->getFunction() && AI->isStaticAlloca(); |
| 2178 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 2179 | return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() || |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 2180 | GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) && |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2181 | !GV->isThreadLocal(); |
| 2182 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 2183 | return A->hasByValAttr(); |
| 2184 | return false; |
| 2185 | }); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2186 | }; |
| 2187 | |
| 2188 | if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) || |
| 2189 | (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs))) |
| 2190 | return ConstantInt::get(GetCompareTy(LHS), |
| 2191 | !CmpInst::isTrueWhenEqual(Pred)); |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2192 | |
| 2193 | // Fold comparisons for non-escaping pointer even if the allocation call |
| 2194 | // cannot be elided. We cannot fold malloc comparison to null. Also, the |
| 2195 | // dynamic allocation call could be either of the operands. |
| 2196 | Value *MI = nullptr; |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2197 | if (isAllocLikeFn(LHS, TLI) && |
| 2198 | llvm::isKnownNonZero(RHS, DL, 0, nullptr, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2199 | MI = LHS; |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2200 | else if (isAllocLikeFn(RHS, TLI) && |
| 2201 | llvm::isKnownNonZero(LHS, DL, 0, nullptr, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2202 | MI = RHS; |
| 2203 | // FIXME: We should also fold the compare when the pointer escapes, but the |
| 2204 | // compare dominates the pointer escape |
| 2205 | if (MI && !PointerMayBeCaptured(MI, true, true)) |
| 2206 | return ConstantInt::get(GetCompareTy(LHS), |
| 2207 | CmpInst::isFalseWhenEqual(Pred)); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | // Otherwise, fail. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2211 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2212 | } |
Chris Lattner | 01990f0 | 2012-02-24 19:01:58 +0000 | [diff] [blame] | 2213 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2214 | /// Fold an icmp when its operands have i1 scalar type. |
| 2215 | static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 2216 | Value *RHS, const SimplifyQuery &Q) { |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2217 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2218 | Type *OpTy = LHS->getType(); // The operand type. |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 2219 | if (!OpTy->isIntOrIntVectorTy(1)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2220 | return nullptr; |
| 2221 | |
Sanjay Patel | e2787b9 | 2017-05-17 20:27:55 +0000 | [diff] [blame] | 2222 | // A boolean compared to true/false can be simplified in 14 out of the 20 |
| 2223 | // (10 predicates * 2 constants) possible combinations. Cases not handled here |
| 2224 | // require a 'not' of the LHS, so those must be transformed in InstCombine. |
| 2225 | if (match(RHS, m_Zero())) { |
| 2226 | switch (Pred) { |
| 2227 | case CmpInst::ICMP_NE: // X != 0 -> X |
| 2228 | case CmpInst::ICMP_UGT: // X >u 0 -> X |
| 2229 | case CmpInst::ICMP_SLT: // X <s 0 -> X |
| 2230 | return LHS; |
| 2231 | |
| 2232 | case CmpInst::ICMP_ULT: // X <u 0 -> false |
| 2233 | case CmpInst::ICMP_SGT: // X >s 0 -> false |
| 2234 | return getFalse(ITy); |
| 2235 | |
| 2236 | case CmpInst::ICMP_UGE: // X >=u 0 -> true |
| 2237 | case CmpInst::ICMP_SLE: // X <=s 0 -> true |
| 2238 | return getTrue(ITy); |
| 2239 | |
| 2240 | default: break; |
| 2241 | } |
| 2242 | } else if (match(RHS, m_One())) { |
| 2243 | switch (Pred) { |
| 2244 | case CmpInst::ICMP_EQ: // X == 1 -> X |
| 2245 | case CmpInst::ICMP_UGE: // X >=u 1 -> X |
| 2246 | case CmpInst::ICMP_SLE: // X <=s -1 -> X |
| 2247 | return LHS; |
| 2248 | |
| 2249 | case CmpInst::ICMP_UGT: // X >u 1 -> false |
| 2250 | case CmpInst::ICMP_SLT: // X <s -1 -> false |
| 2251 | return getFalse(ITy); |
| 2252 | |
| 2253 | case CmpInst::ICMP_ULE: // X <=u 1 -> true |
| 2254 | case CmpInst::ICMP_SGE: // X >=s -1 -> true |
| 2255 | return getTrue(ITy); |
| 2256 | |
| 2257 | default: break; |
| 2258 | } |
| 2259 | } |
| 2260 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2261 | switch (Pred) { |
| 2262 | default: |
| 2263 | break; |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2264 | case ICmpInst::ICMP_UGE: |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2265 | if (isImpliedCondition(RHS, LHS, Q.DL).getValueOr(false)) |
| 2266 | return getTrue(ITy); |
| 2267 | break; |
| 2268 | case ICmpInst::ICMP_SGE: |
| 2269 | /// For signed comparison, the values for an i1 are 0 and -1 |
| 2270 | /// respectively. This maps into a truth table of: |
| 2271 | /// LHS | RHS | LHS >=s RHS | LHS implies RHS |
| 2272 | /// 0 | 0 | 1 (0 >= 0) | 1 |
| 2273 | /// 0 | 1 | 1 (0 >= -1) | 1 |
| 2274 | /// 1 | 0 | 0 (-1 >= 0) | 0 |
| 2275 | /// 1 | 1 | 1 (-1 >= -1) | 1 |
| 2276 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
| 2277 | return getTrue(ITy); |
| 2278 | break; |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2279 | case ICmpInst::ICMP_ULE: |
| 2280 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
| 2281 | return getTrue(ITy); |
| 2282 | break; |
| 2283 | } |
| 2284 | |
| 2285 | return nullptr; |
| 2286 | } |
| 2287 | |
| 2288 | /// Try hard to fold icmp with zero RHS because this is a common case. |
| 2289 | static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 2290 | Value *RHS, const SimplifyQuery &Q) { |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2291 | if (!match(RHS, m_Zero())) |
| 2292 | return nullptr; |
| 2293 | |
| 2294 | Type *ITy = GetCompareTy(LHS); // The return type. |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2295 | switch (Pred) { |
| 2296 | default: |
| 2297 | llvm_unreachable("Unknown ICmp predicate!"); |
| 2298 | case ICmpInst::ICMP_ULT: |
| 2299 | return getFalse(ITy); |
| 2300 | case ICmpInst::ICMP_UGE: |
| 2301 | return getTrue(ITy); |
| 2302 | case ICmpInst::ICMP_EQ: |
| 2303 | case ICmpInst::ICMP_ULE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2304 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2305 | return getFalse(ITy); |
| 2306 | break; |
| 2307 | case ICmpInst::ICMP_NE: |
| 2308 | case ICmpInst::ICMP_UGT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2309 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2310 | return getTrue(ITy); |
| 2311 | break; |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2312 | case ICmpInst::ICMP_SLT: { |
| 2313 | KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2314 | if (LHSKnown.isNegative()) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2315 | return getTrue(ITy); |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2316 | if (LHSKnown.isNonNegative()) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2317 | return getFalse(ITy); |
| 2318 | break; |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2319 | } |
| 2320 | case ICmpInst::ICMP_SLE: { |
| 2321 | KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2322 | if (LHSKnown.isNegative()) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2323 | return getTrue(ITy); |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2324 | if (LHSKnown.isNonNegative() && |
| 2325 | isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2326 | return getFalse(ITy); |
| 2327 | break; |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2328 | } |
| 2329 | case ICmpInst::ICMP_SGE: { |
| 2330 | KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2331 | if (LHSKnown.isNegative()) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2332 | return getFalse(ITy); |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2333 | if (LHSKnown.isNonNegative()) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2334 | return getTrue(ITy); |
| 2335 | break; |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2336 | } |
| 2337 | case ICmpInst::ICMP_SGT: { |
| 2338 | KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2339 | if (LHSKnown.isNegative()) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2340 | return getFalse(ITy); |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2341 | if (LHSKnown.isNonNegative() && |
| 2342 | isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2343 | return getTrue(ITy); |
| 2344 | break; |
| 2345 | } |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2346 | } |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2347 | |
| 2348 | return nullptr; |
| 2349 | } |
| 2350 | |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2351 | /// Many binary operators with a constant operand have an easy-to-compute |
| 2352 | /// range of outputs. This can be used to fold a comparison to always true or |
| 2353 | /// always false. |
| 2354 | static void setLimitsForBinOp(BinaryOperator &BO, APInt &Lower, APInt &Upper) { |
| 2355 | unsigned Width = Lower.getBitWidth(); |
| 2356 | const APInt *C; |
| 2357 | switch (BO.getOpcode()) { |
| 2358 | case Instruction::Add: |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2359 | if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) { |
Sanjay Patel | 5622725 | 2017-01-24 17:03:24 +0000 | [diff] [blame] | 2360 | // FIXME: If we have both nuw and nsw, we should reduce the range further. |
| 2361 | if (BO.hasNoUnsignedWrap()) { |
| 2362 | // 'add nuw x, C' produces [C, UINT_MAX]. |
| 2363 | Lower = *C; |
| 2364 | } else if (BO.hasNoSignedWrap()) { |
| 2365 | if (C->isNegative()) { |
| 2366 | // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. |
| 2367 | Lower = APInt::getSignedMinValue(Width); |
| 2368 | Upper = APInt::getSignedMaxValue(Width) + *C + 1; |
| 2369 | } else { |
| 2370 | // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. |
| 2371 | Lower = APInt::getSignedMinValue(Width) + *C; |
| 2372 | Upper = APInt::getSignedMaxValue(Width) + 1; |
| 2373 | } |
| 2374 | } |
| 2375 | } |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2376 | break; |
| 2377 | |
| 2378 | case Instruction::And: |
| 2379 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2380 | // 'and x, C' produces [0, C]. |
| 2381 | Upper = *C + 1; |
| 2382 | break; |
| 2383 | |
| 2384 | case Instruction::Or: |
| 2385 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2386 | // 'or x, C' produces [C, UINT_MAX]. |
| 2387 | Lower = *C; |
| 2388 | break; |
| 2389 | |
| 2390 | case Instruction::AShr: |
| 2391 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 2392 | // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. |
| 2393 | Lower = APInt::getSignedMinValue(Width).ashr(*C); |
| 2394 | Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1; |
| 2395 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2396 | unsigned ShiftAmount = Width - 1; |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2397 | if (!C->isNullValue() && BO.isExact()) |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2398 | ShiftAmount = C->countTrailingZeros(); |
| 2399 | if (C->isNegative()) { |
| 2400 | // 'ashr C, x' produces [C, C >> (Width-1)] |
| 2401 | Lower = *C; |
| 2402 | Upper = C->ashr(ShiftAmount) + 1; |
| 2403 | } else { |
| 2404 | // 'ashr C, x' produces [C >> (Width-1), C] |
| 2405 | Lower = C->ashr(ShiftAmount); |
| 2406 | Upper = *C + 1; |
| 2407 | } |
| 2408 | } |
| 2409 | break; |
| 2410 | |
| 2411 | case Instruction::LShr: |
| 2412 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 2413 | // 'lshr x, C' produces [0, UINT_MAX >> C]. |
| 2414 | Upper = APInt::getAllOnesValue(Width).lshr(*C) + 1; |
| 2415 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2416 | // 'lshr C, x' produces [C >> (Width-1), C]. |
| 2417 | unsigned ShiftAmount = Width - 1; |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2418 | if (!C->isNullValue() && BO.isExact()) |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2419 | ShiftAmount = C->countTrailingZeros(); |
| 2420 | Lower = C->lshr(ShiftAmount); |
| 2421 | Upper = *C + 1; |
| 2422 | } |
| 2423 | break; |
| 2424 | |
| 2425 | case Instruction::Shl: |
| 2426 | if (match(BO.getOperand(0), m_APInt(C))) { |
| 2427 | if (BO.hasNoUnsignedWrap()) { |
| 2428 | // 'shl nuw C, x' produces [C, C << CLZ(C)] |
| 2429 | Lower = *C; |
| 2430 | Upper = Lower.shl(Lower.countLeadingZeros()) + 1; |
| 2431 | } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw? |
| 2432 | if (C->isNegative()) { |
| 2433 | // 'shl nsw C, x' produces [C << CLO(C)-1, C] |
| 2434 | unsigned ShiftAmount = C->countLeadingOnes() - 1; |
| 2435 | Lower = C->shl(ShiftAmount); |
| 2436 | Upper = *C + 1; |
| 2437 | } else { |
| 2438 | // 'shl nsw C, x' produces [C, C << CLZ(C)-1] |
| 2439 | unsigned ShiftAmount = C->countLeadingZeros() - 1; |
| 2440 | Lower = *C; |
| 2441 | Upper = C->shl(ShiftAmount) + 1; |
| 2442 | } |
| 2443 | } |
| 2444 | } |
| 2445 | break; |
| 2446 | |
| 2447 | case Instruction::SDiv: |
| 2448 | if (match(BO.getOperand(1), m_APInt(C))) { |
| 2449 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2450 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 2451 | if (C->isAllOnesValue()) { |
| 2452 | // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] |
| 2453 | // where C != -1 and C != 0 and C != 1 |
| 2454 | Lower = IntMin + 1; |
| 2455 | Upper = IntMax + 1; |
| 2456 | } else if (C->countLeadingZeros() < Width - 1) { |
| 2457 | // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] |
| 2458 | // where C != -1 and C != 0 and C != 1 |
| 2459 | Lower = IntMin.sdiv(*C); |
| 2460 | Upper = IntMax.sdiv(*C); |
| 2461 | if (Lower.sgt(Upper)) |
| 2462 | std::swap(Lower, Upper); |
| 2463 | Upper = Upper + 1; |
| 2464 | assert(Upper != Lower && "Upper part of range has wrapped!"); |
| 2465 | } |
| 2466 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2467 | if (C->isMinSignedValue()) { |
| 2468 | // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. |
| 2469 | Lower = *C; |
| 2470 | Upper = Lower.lshr(1) + 1; |
| 2471 | } else { |
| 2472 | // 'sdiv C, x' produces [-|C|, |C|]. |
| 2473 | Upper = C->abs() + 1; |
| 2474 | Lower = (-Upper) + 1; |
| 2475 | } |
| 2476 | } |
| 2477 | break; |
| 2478 | |
| 2479 | case Instruction::UDiv: |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2480 | if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) { |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2481 | // 'udiv x, C' produces [0, UINT_MAX / C]. |
| 2482 | Upper = APInt::getMaxValue(Width).udiv(*C) + 1; |
| 2483 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2484 | // 'udiv C, x' produces [0, C]. |
| 2485 | Upper = *C + 1; |
| 2486 | } |
| 2487 | break; |
| 2488 | |
| 2489 | case Instruction::SRem: |
| 2490 | if (match(BO.getOperand(1), m_APInt(C))) { |
| 2491 | // 'srem x, C' produces (-|C|, |C|). |
| 2492 | Upper = C->abs(); |
| 2493 | Lower = (-Upper) + 1; |
| 2494 | } |
| 2495 | break; |
| 2496 | |
| 2497 | case Instruction::URem: |
| 2498 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2499 | // 'urem x, C' produces [0, C). |
| 2500 | Upper = *C; |
| 2501 | break; |
| 2502 | |
| 2503 | default: |
| 2504 | break; |
| 2505 | } |
| 2506 | } |
| 2507 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2508 | static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS, |
| 2509 | Value *RHS) { |
Roman Lebedev | 0c43d72 | 2018-03-15 16:17:40 +0000 | [diff] [blame] | 2510 | Type *ITy = GetCompareTy(RHS); // The return type. |
| 2511 | |
Roman Lebedev | 6aca335 | 2018-03-15 16:17:46 +0000 | [diff] [blame] | 2512 | Value *X; |
| 2513 | // Sign-bit checks can be optimized to true/false after unsigned |
| 2514 | // floating-point casts: |
| 2515 | // icmp slt (bitcast (uitofp X)), 0 --> false |
| 2516 | // icmp sgt (bitcast (uitofp X)), -1 --> true |
| 2517 | if (match(LHS, m_BitCast(m_UIToFP(m_Value(X))))) { |
| 2518 | if (Pred == ICmpInst::ICMP_SLT && match(RHS, m_Zero())) |
| 2519 | return ConstantInt::getFalse(ITy); |
| 2520 | if (Pred == ICmpInst::ICMP_SGT && match(RHS, m_AllOnes())) |
| 2521 | return ConstantInt::getTrue(ITy); |
| 2522 | } |
| 2523 | |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2524 | const APInt *C; |
| 2525 | if (!match(RHS, m_APInt(C))) |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2526 | return nullptr; |
| 2527 | |
| 2528 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
Sanjoy Das | 1f7b813 | 2016-10-02 00:09:57 +0000 | [diff] [blame] | 2529 | ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2530 | if (RHS_CR.isEmptySet()) |
Roman Lebedev | 0c43d72 | 2018-03-15 16:17:40 +0000 | [diff] [blame] | 2531 | return ConstantInt::getFalse(ITy); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2532 | if (RHS_CR.isFullSet()) |
Roman Lebedev | 0c43d72 | 2018-03-15 16:17:40 +0000 | [diff] [blame] | 2533 | return ConstantInt::getTrue(ITy); |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2534 | |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2535 | // Find the range of possible values for binary operators. |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2536 | unsigned Width = C->getBitWidth(); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2537 | APInt Lower = APInt(Width, 0); |
| 2538 | APInt Upper = APInt(Width, 0); |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2539 | if (auto *BO = dyn_cast<BinaryOperator>(LHS)) |
| 2540 | setLimitsForBinOp(*BO, Lower, Upper); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2541 | |
| 2542 | ConstantRange LHS_CR = |
| 2543 | Lower != Upper ? ConstantRange(Lower, Upper) : ConstantRange(Width, true); |
| 2544 | |
| 2545 | if (auto *I = dyn_cast<Instruction>(LHS)) |
| 2546 | if (auto *Ranges = I->getMetadata(LLVMContext::MD_range)) |
| 2547 | LHS_CR = LHS_CR.intersectWith(getConstantRangeFromMetadata(*Ranges)); |
| 2548 | |
| 2549 | if (!LHS_CR.isFullSet()) { |
| 2550 | if (RHS_CR.contains(LHS_CR)) |
Roman Lebedev | 0c43d72 | 2018-03-15 16:17:40 +0000 | [diff] [blame] | 2551 | return ConstantInt::getTrue(ITy); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2552 | if (RHS_CR.inverse().contains(LHS_CR)) |
Roman Lebedev | 0c43d72 | 2018-03-15 16:17:40 +0000 | [diff] [blame] | 2553 | return ConstantInt::getFalse(ITy); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | return nullptr; |
| 2557 | } |
| 2558 | |
Sanjay Patel | 2df38a8 | 2017-05-08 16:21:55 +0000 | [diff] [blame] | 2559 | /// TODO: A large part of this logic is duplicated in InstCombine's |
| 2560 | /// foldICmpBinOp(). We should be able to share that and avoid the code |
| 2561 | /// duplication. |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2562 | static Value *simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 2563 | Value *RHS, const SimplifyQuery &Q, |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2564 | unsigned MaxRecurse) { |
| 2565 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2566 | |
| 2567 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 2568 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 2569 | if (MaxRecurse && (LBO || RBO)) { |
| 2570 | // Analyze the case when either LHS or RHS is an add instruction. |
| 2571 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
| 2572 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 2573 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 2574 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 2575 | A = LBO->getOperand(0); |
| 2576 | B = LBO->getOperand(1); |
| 2577 | NoLHSWrapProblem = |
| 2578 | ICmpInst::isEquality(Pred) || |
| 2579 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 2580 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 2581 | } |
| 2582 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 2583 | C = RBO->getOperand(0); |
| 2584 | D = RBO->getOperand(1); |
| 2585 | NoRHSWrapProblem = |
| 2586 | ICmpInst::isEquality(Pred) || |
| 2587 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 2588 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 2589 | } |
| 2590 | |
| 2591 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2592 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 2593 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 2594 | Constant::getNullValue(RHS->getType()), Q, |
| 2595 | MaxRecurse - 1)) |
| 2596 | return V; |
| 2597 | |
| 2598 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2599 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 2600 | if (Value *V = |
| 2601 | SimplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()), |
| 2602 | C == LHS ? D : C, Q, MaxRecurse - 1)) |
| 2603 | return V; |
| 2604 | |
| 2605 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 2606 | if (A && C && (A == C || A == D || B == C || B == D) && NoLHSWrapProblem && |
| 2607 | NoRHSWrapProblem) { |
| 2608 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
| 2609 | Value *Y, *Z; |
| 2610 | if (A == C) { |
| 2611 | // C + B == C + D -> B == D |
| 2612 | Y = B; |
| 2613 | Z = D; |
| 2614 | } else if (A == D) { |
| 2615 | // D + B == C + D -> B == C |
| 2616 | Y = B; |
| 2617 | Z = C; |
| 2618 | } else if (B == C) { |
| 2619 | // A + C == C + D -> A == D |
| 2620 | Y = A; |
| 2621 | Z = D; |
| 2622 | } else { |
| 2623 | assert(B == D); |
| 2624 | // A + D == C + D -> A == C |
| 2625 | Y = A; |
| 2626 | Z = C; |
| 2627 | } |
| 2628 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1)) |
| 2629 | return V; |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | { |
| 2634 | Value *Y = nullptr; |
| 2635 | // icmp pred (or X, Y), X |
| 2636 | if (LBO && match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) { |
| 2637 | if (Pred == ICmpInst::ICMP_ULT) |
| 2638 | return getFalse(ITy); |
| 2639 | if (Pred == ICmpInst::ICMP_UGE) |
| 2640 | return getTrue(ITy); |
| 2641 | |
| 2642 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) { |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2643 | KnownBits RHSKnown = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2644 | KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2645 | if (RHSKnown.isNonNegative() && YKnown.isNegative()) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2646 | return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy); |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2647 | if (RHSKnown.isNegative() || YKnown.isNonNegative()) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2648 | return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy); |
| 2649 | } |
| 2650 | } |
| 2651 | // icmp pred X, (or X, Y) |
| 2652 | if (RBO && match(RBO, m_c_Or(m_Value(Y), m_Specific(LHS)))) { |
| 2653 | if (Pred == ICmpInst::ICMP_ULE) |
| 2654 | return getTrue(ITy); |
| 2655 | if (Pred == ICmpInst::ICMP_UGT) |
| 2656 | return getFalse(ITy); |
| 2657 | |
| 2658 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLE) { |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2659 | KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2660 | KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2661 | if (LHSKnown.isNonNegative() && YKnown.isNegative()) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2662 | return Pred == ICmpInst::ICMP_SGT ? getTrue(ITy) : getFalse(ITy); |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2663 | if (LHSKnown.isNegative() || YKnown.isNonNegative()) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2664 | return Pred == ICmpInst::ICMP_SGT ? getFalse(ITy) : getTrue(ITy); |
| 2665 | } |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | // icmp pred (and X, Y), X |
Craig Topper | 72ee694 | 2017-06-24 06:24:01 +0000 | [diff] [blame] | 2670 | if (LBO && match(LBO, m_c_And(m_Value(), m_Specific(RHS)))) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2671 | if (Pred == ICmpInst::ICMP_UGT) |
| 2672 | return getFalse(ITy); |
| 2673 | if (Pred == ICmpInst::ICMP_ULE) |
| 2674 | return getTrue(ITy); |
| 2675 | } |
| 2676 | // icmp pred X, (and X, Y) |
Craig Topper | 72ee694 | 2017-06-24 06:24:01 +0000 | [diff] [blame] | 2677 | if (RBO && match(RBO, m_c_And(m_Value(), m_Specific(LHS)))) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2678 | if (Pred == ICmpInst::ICMP_UGE) |
| 2679 | return getTrue(ITy); |
| 2680 | if (Pred == ICmpInst::ICMP_ULT) |
| 2681 | return getFalse(ITy); |
| 2682 | } |
| 2683 | |
| 2684 | // 0 - (zext X) pred C |
| 2685 | if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) { |
| 2686 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2687 | if (RHSC->getValue().isStrictlyPositive()) { |
| 2688 | if (Pred == ICmpInst::ICMP_SLT) |
| 2689 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2690 | if (Pred == ICmpInst::ICMP_SGE) |
| 2691 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2692 | if (Pred == ICmpInst::ICMP_EQ) |
| 2693 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2694 | if (Pred == ICmpInst::ICMP_NE) |
| 2695 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2696 | } |
| 2697 | if (RHSC->getValue().isNonNegative()) { |
| 2698 | if (Pred == ICmpInst::ICMP_SLE) |
| 2699 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2700 | if (Pred == ICmpInst::ICMP_SGT) |
| 2701 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2702 | } |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | // icmp pred (urem X, Y), Y |
| 2707 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2708 | switch (Pred) { |
| 2709 | default: |
| 2710 | break; |
| 2711 | case ICmpInst::ICMP_SGT: |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2712 | case ICmpInst::ICMP_SGE: { |
| 2713 | KnownBits Known = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2714 | if (!Known.isNonNegative()) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2715 | break; |
| 2716 | LLVM_FALLTHROUGH; |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2717 | } |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2718 | case ICmpInst::ICMP_EQ: |
| 2719 | case ICmpInst::ICMP_UGT: |
| 2720 | case ICmpInst::ICMP_UGE: |
| 2721 | return getFalse(ITy); |
| 2722 | case ICmpInst::ICMP_SLT: |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2723 | case ICmpInst::ICMP_SLE: { |
| 2724 | KnownBits Known = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2725 | if (!Known.isNonNegative()) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2726 | break; |
| 2727 | LLVM_FALLTHROUGH; |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2728 | } |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2729 | case ICmpInst::ICMP_NE: |
| 2730 | case ICmpInst::ICMP_ULT: |
| 2731 | case ICmpInst::ICMP_ULE: |
| 2732 | return getTrue(ITy); |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | // icmp pred X, (urem Y, X) |
| 2737 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2738 | switch (Pred) { |
| 2739 | default: |
| 2740 | break; |
| 2741 | case ICmpInst::ICMP_SGT: |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2742 | case ICmpInst::ICMP_SGE: { |
| 2743 | KnownBits Known = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2744 | if (!Known.isNonNegative()) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2745 | break; |
| 2746 | LLVM_FALLTHROUGH; |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2747 | } |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2748 | case ICmpInst::ICMP_NE: |
| 2749 | case ICmpInst::ICMP_UGT: |
| 2750 | case ICmpInst::ICMP_UGE: |
| 2751 | return getTrue(ITy); |
| 2752 | case ICmpInst::ICMP_SLT: |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2753 | case ICmpInst::ICMP_SLE: { |
| 2754 | KnownBits Known = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 2755 | if (!Known.isNonNegative()) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2756 | break; |
| 2757 | LLVM_FALLTHROUGH; |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 2758 | } |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2759 | case ICmpInst::ICMP_EQ: |
| 2760 | case ICmpInst::ICMP_ULT: |
| 2761 | case ICmpInst::ICMP_ULE: |
| 2762 | return getFalse(ITy); |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | // x >> y <=u x |
| 2767 | // x udiv y <=u x. |
| 2768 | if (LBO && (match(LBO, m_LShr(m_Specific(RHS), m_Value())) || |
| 2769 | match(LBO, m_UDiv(m_Specific(RHS), m_Value())))) { |
| 2770 | // icmp pred (X op Y), X |
| 2771 | if (Pred == ICmpInst::ICMP_UGT) |
| 2772 | return getFalse(ITy); |
| 2773 | if (Pred == ICmpInst::ICMP_ULE) |
| 2774 | return getTrue(ITy); |
| 2775 | } |
| 2776 | |
| 2777 | // x >=u x >> y |
| 2778 | // x >=u x udiv y. |
| 2779 | if (RBO && (match(RBO, m_LShr(m_Specific(LHS), m_Value())) || |
| 2780 | match(RBO, m_UDiv(m_Specific(LHS), m_Value())))) { |
| 2781 | // icmp pred X, (X op Y) |
| 2782 | if (Pred == ICmpInst::ICMP_ULT) |
| 2783 | return getFalse(ITy); |
| 2784 | if (Pred == ICmpInst::ICMP_UGE) |
| 2785 | return getTrue(ITy); |
| 2786 | } |
| 2787 | |
| 2788 | // handle: |
| 2789 | // CI2 << X == CI |
| 2790 | // CI2 << X != CI |
| 2791 | // |
| 2792 | // where CI2 is a power of 2 and CI isn't |
| 2793 | if (auto *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2794 | const APInt *CI2Val, *CIVal = &CI->getValue(); |
| 2795 | if (LBO && match(LBO, m_Shl(m_APInt(CI2Val), m_Value())) && |
| 2796 | CI2Val->isPowerOf2()) { |
| 2797 | if (!CIVal->isPowerOf2()) { |
| 2798 | // CI2 << X can equal zero in some circumstances, |
| 2799 | // this simplification is unsafe if CI is zero. |
| 2800 | // |
| 2801 | // We know it is safe if: |
| 2802 | // - The shift is nsw, we can't shift out the one bit. |
| 2803 | // - The shift is nuw, we can't shift out the one bit. |
| 2804 | // - CI2 is one |
| 2805 | // - CI isn't zero |
| 2806 | if (LBO->hasNoSignedWrap() || LBO->hasNoUnsignedWrap() || |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2807 | CI2Val->isOneValue() || !CI->isZero()) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2808 | if (Pred == ICmpInst::ICMP_EQ) |
| 2809 | return ConstantInt::getFalse(RHS->getContext()); |
| 2810 | if (Pred == ICmpInst::ICMP_NE) |
| 2811 | return ConstantInt::getTrue(RHS->getContext()); |
| 2812 | } |
| 2813 | } |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2814 | if (CIVal->isSignMask() && CI2Val->isOneValue()) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2815 | if (Pred == ICmpInst::ICMP_UGT) |
| 2816 | return ConstantInt::getFalse(RHS->getContext()); |
| 2817 | if (Pred == ICmpInst::ICMP_ULE) |
| 2818 | return ConstantInt::getTrue(RHS->getContext()); |
| 2819 | } |
| 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 2824 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 2825 | switch (LBO->getOpcode()) { |
| 2826 | default: |
| 2827 | break; |
| 2828 | case Instruction::UDiv: |
| 2829 | case Instruction::LShr: |
Sanjay Patel | a23b141 | 2017-05-15 19:16:49 +0000 | [diff] [blame] | 2830 | if (ICmpInst::isSigned(Pred) || !LBO->isExact() || !RBO->isExact()) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2831 | break; |
Sanjay Patel | a23b141 | 2017-05-15 19:16:49 +0000 | [diff] [blame] | 2832 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2833 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2834 | return V; |
| 2835 | break; |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2836 | case Instruction::SDiv: |
Sanjay Patel | a23b141 | 2017-05-15 19:16:49 +0000 | [diff] [blame] | 2837 | if (!ICmpInst::isEquality(Pred) || !LBO->isExact() || !RBO->isExact()) |
| 2838 | break; |
| 2839 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2840 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2841 | return V; |
| 2842 | break; |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2843 | case Instruction::AShr: |
| 2844 | if (!LBO->isExact() || !RBO->isExact()) |
| 2845 | break; |
| 2846 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2847 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2848 | return V; |
| 2849 | break; |
| 2850 | case Instruction::Shl: { |
| 2851 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
| 2852 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2853 | if (!NUW && !NSW) |
| 2854 | break; |
| 2855 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2856 | break; |
| 2857 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2858 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2859 | return V; |
| 2860 | break; |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | return nullptr; |
| 2865 | } |
| 2866 | |
Sanjay Patel | 35289c6 | 2016-12-10 17:40:47 +0000 | [diff] [blame] | 2867 | /// Simplify integer comparisons where at least one operand of the compare |
| 2868 | /// matches an integer min/max idiom. |
| 2869 | static Value *simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 2870 | Value *RHS, const SimplifyQuery &Q, |
Sanjay Patel | 35289c6 | 2016-12-10 17:40:47 +0000 | [diff] [blame] | 2871 | unsigned MaxRecurse) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2872 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2873 | Value *A, *B; |
| 2874 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
| 2875 | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
| 2876 | |
| 2877 | // Signed variants on "max(a,b)>=a -> true". |
| 2878 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2879 | if (A != RHS) |
| 2880 | std::swap(A, B); // smax(A, B) pred A. |
| 2881 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2882 | // We analyze this as smax(A, B) pred A. |
| 2883 | P = Pred; |
| 2884 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2885 | (A == LHS || B == LHS)) { |
| 2886 | if (A != LHS) |
| 2887 | std::swap(A, B); // A pred smax(A, B). |
| 2888 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2889 | // We analyze this as smax(A, B) swapped-pred A. |
| 2890 | P = CmpInst::getSwappedPredicate(Pred); |
| 2891 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2892 | (A == RHS || B == RHS)) { |
| 2893 | if (A != RHS) |
| 2894 | std::swap(A, B); // smin(A, B) pred A. |
| 2895 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2896 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2897 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2898 | P = CmpInst::getSwappedPredicate(Pred); |
| 2899 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2900 | (A == LHS || B == LHS)) { |
| 2901 | if (A != LHS) |
| 2902 | std::swap(A, B); // A pred smin(A, B). |
| 2903 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2904 | // We analyze this as smax(-A, -B) pred -A. |
| 2905 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2906 | P = Pred; |
| 2907 | } |
| 2908 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2909 | // Cases correspond to "max(A, B) p A". |
| 2910 | switch (P) { |
| 2911 | default: |
| 2912 | break; |
| 2913 | case CmpInst::ICMP_EQ: |
| 2914 | case CmpInst::ICMP_SLE: |
| 2915 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2916 | // in the max/min; if so, we can just return that. |
| 2917 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2918 | return V; |
| 2919 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2920 | return V; |
| 2921 | // Otherwise, see if "A EqP B" simplifies. |
| 2922 | if (MaxRecurse) |
| 2923 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
| 2924 | return V; |
| 2925 | break; |
| 2926 | case CmpInst::ICMP_NE: |
| 2927 | case CmpInst::ICMP_SGT: { |
| 2928 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2929 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2930 | // tested in the max/min; if so, we can just return that. |
| 2931 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2932 | return V; |
| 2933 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2934 | return V; |
| 2935 | // Otherwise, see if "A InvEqP B" simplifies. |
| 2936 | if (MaxRecurse) |
| 2937 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
| 2938 | return V; |
| 2939 | break; |
| 2940 | } |
| 2941 | case CmpInst::ICMP_SGE: |
| 2942 | // Always true. |
| 2943 | return getTrue(ITy); |
| 2944 | case CmpInst::ICMP_SLT: |
| 2945 | // Always false. |
| 2946 | return getFalse(ITy); |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | // Unsigned variants on "max(a,b)>=a -> true". |
| 2951 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2952 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2953 | if (A != RHS) |
| 2954 | std::swap(A, B); // umax(A, B) pred A. |
| 2955 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2956 | // We analyze this as umax(A, B) pred A. |
| 2957 | P = Pred; |
| 2958 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2959 | (A == LHS || B == LHS)) { |
| 2960 | if (A != LHS) |
| 2961 | std::swap(A, B); // A pred umax(A, B). |
| 2962 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2963 | // We analyze this as umax(A, B) swapped-pred A. |
| 2964 | P = CmpInst::getSwappedPredicate(Pred); |
| 2965 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2966 | (A == RHS || B == RHS)) { |
| 2967 | if (A != RHS) |
| 2968 | std::swap(A, B); // umin(A, B) pred A. |
| 2969 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2970 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 2971 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2972 | P = CmpInst::getSwappedPredicate(Pred); |
| 2973 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2974 | (A == LHS || B == LHS)) { |
| 2975 | if (A != LHS) |
| 2976 | std::swap(A, B); // A pred umin(A, B). |
| 2977 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2978 | // We analyze this as umax(-A, -B) pred -A. |
| 2979 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2980 | P = Pred; |
| 2981 | } |
| 2982 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2983 | // Cases correspond to "max(A, B) p A". |
| 2984 | switch (P) { |
| 2985 | default: |
| 2986 | break; |
| 2987 | case CmpInst::ICMP_EQ: |
| 2988 | case CmpInst::ICMP_ULE: |
| 2989 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2990 | // in the max/min; if so, we can just return that. |
| 2991 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2992 | return V; |
| 2993 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2994 | return V; |
| 2995 | // Otherwise, see if "A EqP B" simplifies. |
| 2996 | if (MaxRecurse) |
| 2997 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
| 2998 | return V; |
| 2999 | break; |
| 3000 | case CmpInst::ICMP_NE: |
| 3001 | case CmpInst::ICMP_UGT: { |
| 3002 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 3003 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 3004 | // tested in the max/min; if so, we can just return that. |
| 3005 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 3006 | return V; |
| 3007 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 3008 | return V; |
| 3009 | // Otherwise, see if "A InvEqP B" simplifies. |
| 3010 | if (MaxRecurse) |
| 3011 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
| 3012 | return V; |
| 3013 | break; |
| 3014 | } |
| 3015 | case CmpInst::ICMP_UGE: |
| 3016 | // Always true. |
| 3017 | return getTrue(ITy); |
| 3018 | case CmpInst::ICMP_ULT: |
| 3019 | // Always false. |
| 3020 | return getFalse(ITy); |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | // Variants on "max(x,y) >= min(x,z)". |
| 3025 | Value *C, *D; |
| 3026 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 3027 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 3028 | (A == C || A == D || B == C || B == D)) { |
| 3029 | // max(x, ?) pred min(x, ?). |
| 3030 | if (Pred == CmpInst::ICMP_SGE) |
| 3031 | // Always true. |
| 3032 | return getTrue(ITy); |
| 3033 | if (Pred == CmpInst::ICMP_SLT) |
| 3034 | // Always false. |
| 3035 | return getFalse(ITy); |
| 3036 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 3037 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 3038 | (A == C || A == D || B == C || B == D)) { |
| 3039 | // min(x, ?) pred max(x, ?). |
| 3040 | if (Pred == CmpInst::ICMP_SLE) |
| 3041 | // Always true. |
| 3042 | return getTrue(ITy); |
| 3043 | if (Pred == CmpInst::ICMP_SGT) |
| 3044 | // Always false. |
| 3045 | return getFalse(ITy); |
| 3046 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 3047 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 3048 | (A == C || A == D || B == C || B == D)) { |
| 3049 | // max(x, ?) pred min(x, ?). |
| 3050 | if (Pred == CmpInst::ICMP_UGE) |
| 3051 | // Always true. |
| 3052 | return getTrue(ITy); |
| 3053 | if (Pred == CmpInst::ICMP_ULT) |
| 3054 | // Always false. |
| 3055 | return getFalse(ITy); |
| 3056 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 3057 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 3058 | (A == C || A == D || B == C || B == D)) { |
| 3059 | // min(x, ?) pred max(x, ?). |
| 3060 | if (Pred == CmpInst::ICMP_ULE) |
| 3061 | // Always true. |
| 3062 | return getTrue(ITy); |
| 3063 | if (Pred == CmpInst::ICMP_UGT) |
| 3064 | // Always false. |
| 3065 | return getFalse(ITy); |
| 3066 | } |
| 3067 | |
| 3068 | return nullptr; |
| 3069 | } |
| 3070 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3071 | /// Given operands for an ICmpInst, see if we can fold the result. |
| 3072 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3073 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3074 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 3075 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3076 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3077 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3078 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | cdfb80d | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 3079 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3080 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3081 | |
| 3082 | // If we have a constant, make sure it is on the RHS. |
| 3083 | std::swap(LHS, RHS); |
| 3084 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3085 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3086 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3087 | Type *ITy = GetCompareTy(LHS); // The return type. |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3088 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3089 | // icmp X, X -> true/false |
Sanjay Patel | 30be665 | 2018-04-22 17:07:44 +0000 | [diff] [blame] | 3090 | // icmp X, undef -> true/false because undef could be X. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3091 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3092 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3093 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 3094 | if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q)) |
| 3095 | return V; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 3096 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 3097 | if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q)) |
| 3098 | return V; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 3099 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 3100 | if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS)) |
| 3101 | return V; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 3102 | |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3103 | // If both operands have range metadata, use the metadata |
| 3104 | // to simplify the comparison. |
| 3105 | if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) { |
Craig Topper | 0c19861 | 2017-04-10 19:37:10 +0000 | [diff] [blame] | 3106 | auto RHS_Instr = cast<Instruction>(RHS); |
| 3107 | auto LHS_Instr = cast<Instruction>(LHS); |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3108 | |
| 3109 | if (RHS_Instr->getMetadata(LLVMContext::MD_range) && |
| 3110 | LHS_Instr->getMetadata(LLVMContext::MD_range)) { |
Sanjoy Das | a7e1378 | 2015-10-24 05:37:35 +0000 | [diff] [blame] | 3111 | auto RHS_CR = getConstantRangeFromMetadata( |
| 3112 | *RHS_Instr->getMetadata(LLVMContext::MD_range)); |
| 3113 | auto LHS_CR = getConstantRangeFromMetadata( |
| 3114 | *LHS_Instr->getMetadata(LLVMContext::MD_range)); |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3115 | |
| 3116 | auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR); |
| 3117 | if (Satisfied_CR.contains(LHS_CR)) |
| 3118 | return ConstantInt::getTrue(RHS->getContext()); |
| 3119 | |
| 3120 | auto InversedSatisfied_CR = ConstantRange::makeSatisfyingICmpRegion( |
| 3121 | CmpInst::getInversePredicate(Pred), RHS_CR); |
| 3122 | if (InversedSatisfied_CR.contains(LHS_CR)) |
| 3123 | return ConstantInt::getFalse(RHS->getContext()); |
| 3124 | } |
| 3125 | } |
| 3126 | |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3127 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 3128 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 3129 | Instruction *LI = cast<CastInst>(LHS); |
| 3130 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3131 | Type *SrcTy = SrcOp->getType(); |
| 3132 | Type *DstTy = LI->getType(); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3133 | |
| 3134 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 3135 | // if the integer type is the same size as the pointer type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3136 | if (MaxRecurse && isa<PtrToIntInst>(LI) && |
| 3137 | Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) { |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3138 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 3139 | // Transfer the cast to the constant. |
| 3140 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 3141 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3142 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3143 | return V; |
| 3144 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 3145 | if (RI->getOperand(0)->getType() == SrcTy) |
| 3146 | // Compare without the cast. |
| 3147 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3148 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3149 | return V; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | if (isa<ZExtInst>(LHS)) { |
| 3154 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 3155 | // same type. |
| 3156 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 3157 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 3158 | // Compare X and Y. Note that signed predicates become unsigned. |
| 3159 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3160 | SrcOp, RI->getOperand(0), Q, |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3161 | MaxRecurse-1)) |
| 3162 | return V; |
| 3163 | } |
| 3164 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 3165 | // too. If not, then try to deduce the result of the comparison. |
| 3166 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 3167 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3168 | // reextended to DstTy. |
| 3169 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 3170 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 3171 | |
| 3172 | // If the re-extended constant didn't change then this is effectively |
| 3173 | // also a case of comparing two zero-extended values. |
| 3174 | if (RExt == CI && MaxRecurse) |
| 3175 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3176 | SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3177 | return V; |
| 3178 | |
| 3179 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 3180 | // there. Use this to work out the result of the comparison. |
| 3181 | if (RExt != CI) { |
| 3182 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3183 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3184 | // LHS <u RHS. |
| 3185 | case ICmpInst::ICMP_EQ: |
| 3186 | case ICmpInst::ICMP_UGT: |
| 3187 | case ICmpInst::ICMP_UGE: |
| 3188 | return ConstantInt::getFalse(CI->getContext()); |
| 3189 | |
| 3190 | case ICmpInst::ICMP_NE: |
| 3191 | case ICmpInst::ICMP_ULT: |
| 3192 | case ICmpInst::ICMP_ULE: |
| 3193 | return ConstantInt::getTrue(CI->getContext()); |
| 3194 | |
| 3195 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 3196 | // is non-negative then LHS <s RHS. |
| 3197 | case ICmpInst::ICMP_SGT: |
| 3198 | case ICmpInst::ICMP_SGE: |
| 3199 | return CI->getValue().isNegative() ? |
| 3200 | ConstantInt::getTrue(CI->getContext()) : |
| 3201 | ConstantInt::getFalse(CI->getContext()); |
| 3202 | |
| 3203 | case ICmpInst::ICMP_SLT: |
| 3204 | case ICmpInst::ICMP_SLE: |
| 3205 | return CI->getValue().isNegative() ? |
| 3206 | ConstantInt::getFalse(CI->getContext()) : |
| 3207 | ConstantInt::getTrue(CI->getContext()); |
| 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | } |
| 3212 | |
| 3213 | if (isa<SExtInst>(LHS)) { |
| 3214 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 3215 | // same type. |
| 3216 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 3217 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 3218 | // Compare X and Y. Note that the predicate does not change. |
| 3219 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3220 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3221 | return V; |
| 3222 | } |
| 3223 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 3224 | // too. If not, then try to deduce the result of the comparison. |
| 3225 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 3226 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3227 | // reextended to DstTy. |
| 3228 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 3229 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 3230 | |
| 3231 | // If the re-extended constant didn't change then this is effectively |
| 3232 | // also a case of comparing two sign-extended values. |
| 3233 | if (RExt == CI && MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3234 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3235 | return V; |
| 3236 | |
| 3237 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 3238 | // bits there. Use this to work out the result of the comparison. |
| 3239 | if (RExt != CI) { |
| 3240 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3241 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3242 | case ICmpInst::ICMP_EQ: |
| 3243 | return ConstantInt::getFalse(CI->getContext()); |
| 3244 | case ICmpInst::ICMP_NE: |
| 3245 | return ConstantInt::getTrue(CI->getContext()); |
| 3246 | |
| 3247 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 3248 | // LHS >s RHS. |
| 3249 | case ICmpInst::ICMP_SGT: |
| 3250 | case ICmpInst::ICMP_SGE: |
| 3251 | return CI->getValue().isNegative() ? |
| 3252 | ConstantInt::getTrue(CI->getContext()) : |
| 3253 | ConstantInt::getFalse(CI->getContext()); |
| 3254 | case ICmpInst::ICMP_SLT: |
| 3255 | case ICmpInst::ICMP_SLE: |
| 3256 | return CI->getValue().isNegative() ? |
| 3257 | ConstantInt::getFalse(CI->getContext()) : |
| 3258 | ConstantInt::getTrue(CI->getContext()); |
| 3259 | |
| 3260 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 3261 | // LHS >u RHS. |
| 3262 | case ICmpInst::ICMP_UGT: |
| 3263 | case ICmpInst::ICMP_UGE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3264 | // Comparison is true iff the LHS <s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3265 | if (MaxRecurse) |
| 3266 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 3267 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3268 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3269 | return V; |
| 3270 | break; |
| 3271 | case ICmpInst::ICMP_ULT: |
| 3272 | case ICmpInst::ICMP_ULE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3273 | // Comparison is true iff the LHS >=s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3274 | if (MaxRecurse) |
| 3275 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 3276 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3277 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3278 | return V; |
| 3279 | break; |
| 3280 | } |
| 3281 | } |
| 3282 | } |
| 3283 | } |
| 3284 | } |
| 3285 | |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 3286 | // icmp eq|ne X, Y -> false|true if X != Y |
Craig Topper | c2790ec | 2017-06-06 07:13:04 +0000 | [diff] [blame] | 3287 | if (ICmpInst::isEquality(Pred) && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3288 | isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT)) { |
Craig Topper | 2dfb480 | 2017-06-06 07:13:13 +0000 | [diff] [blame] | 3289 | return Pred == ICmpInst::ICMP_NE ? getTrue(ITy) : getFalse(ITy); |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 3290 | } |
Junmo Park | 53470fc | 2016-04-05 21:14:31 +0000 | [diff] [blame] | 3291 | |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 3292 | if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse)) |
| 3293 | return V; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 3294 | |
Sanjay Patel | 35289c6 | 2016-12-10 17:40:47 +0000 | [diff] [blame] | 3295 | if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse)) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 3296 | return V; |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3297 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3298 | // Simplify comparisons of related pointers using a powerful, recursive |
| 3299 | // GEP-walk when we have target data available.. |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 3300 | if (LHS->getType()->isPointerTy()) |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 3301 | if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.AC, Q.CxtI, LHS, |
| 3302 | RHS)) |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3303 | return C; |
David Majnemer | dc8767a | 2016-08-07 07:58:10 +0000 | [diff] [blame] | 3304 | if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS)) |
| 3305 | if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS)) |
| 3306 | if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) == |
| 3307 | Q.DL.getTypeSizeInBits(CLHS->getType()) && |
| 3308 | Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) == |
| 3309 | Q.DL.getTypeSizeInBits(CRHS->getType())) |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 3310 | if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.AC, Q.CxtI, |
David Majnemer | dc8767a | 2016-08-07 07:58:10 +0000 | [diff] [blame] | 3311 | CLHS->getPointerOperand(), |
| 3312 | CRHS->getPointerOperand())) |
| 3313 | return C; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3314 | |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3315 | if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { |
| 3316 | if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { |
| 3317 | if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && |
| 3318 | GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && |
| 3319 | (ICmpInst::isEquality(Pred) || |
| 3320 | (GLHS->isInBounds() && GRHS->isInBounds() && |
| 3321 | Pred == ICmpInst::getSignedPredicate(Pred)))) { |
| 3322 | // The bases are equal and the indices are constant. Build a constant |
| 3323 | // expression GEP with the same indices and a null base pointer to see |
| 3324 | // what constant folding can make out of it. |
| 3325 | Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); |
| 3326 | SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3327 | Constant *NewLHS = ConstantExpr::getGetElementPtr( |
| 3328 | GLHS->getSourceElementType(), Null, IndicesLHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3329 | |
| 3330 | SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3331 | Constant *NewRHS = ConstantExpr::getGetElementPtr( |
| 3332 | GLHS->getSourceElementType(), Null, IndicesRHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3333 | return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); |
| 3334 | } |
| 3335 | } |
| 3336 | } |
| 3337 | |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3338 | // If the comparison is with the result of a select instruction, check whether |
| 3339 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3340 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3341 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3342 | return V; |
| 3343 | |
| 3344 | // If the comparison is with the result of a phi instruction, check whether |
| 3345 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3346 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3347 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3348 | return V; |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3349 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3350 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3353 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3354 | const SimplifyQuery &Q) { |
| 3355 | return ::SimplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit); |
| 3356 | } |
| 3357 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3358 | /// Given operands for an FCmpInst, see if we can fold the result. |
| 3359 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3360 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3361 | FastMathFlags FMF, const SimplifyQuery &Q, |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3362 | unsigned MaxRecurse) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3363 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 3364 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 3365 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3366 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3367 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3368 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3369 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3370 | // If we have a constant, make sure it is on the RHS. |
| 3371 | std::swap(LHS, RHS); |
| 3372 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3373 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3374 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3375 | // Fold trivial predicates. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3376 | Type *RetTy = GetCompareTy(LHS); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3377 | if (Pred == FCmpInst::FCMP_FALSE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3378 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3379 | if (Pred == FCmpInst::FCMP_TRUE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3380 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3381 | |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3382 | // UNO/ORD predicates can be trivially folded if NaNs are ignored. |
| 3383 | if (FMF.noNaNs()) { |
| 3384 | if (Pred == FCmpInst::FCMP_UNO) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3385 | return getFalse(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3386 | if (Pred == FCmpInst::FCMP_ORD) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3387 | return getTrue(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Sanjay Patel | 46b083e | 2018-03-02 18:36:08 +0000 | [diff] [blame] | 3390 | // NaN is unordered; NaN is not ordered. |
| 3391 | assert((FCmpInst::isOrdered(Pred) || FCmpInst::isUnordered(Pred)) && |
| 3392 | "Comparison must be either ordered or unordered"); |
| 3393 | if (match(RHS, m_NaN())) |
| 3394 | return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred)); |
| 3395 | |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3396 | // fcmp pred x, undef and fcmp pred undef, x |
| 3397 | // fold to true if unordered, false if ordered |
| 3398 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) { |
| 3399 | // Choosing NaN for the undef will always make unordered comparison succeed |
| 3400 | // and ordered comparison fail. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3401 | return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred)); |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3402 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3403 | |
| 3404 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3405 | if (LHS == RHS) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3406 | if (CmpInst::isTrueWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3407 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3408 | if (CmpInst::isFalseWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3409 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3410 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3411 | |
Sanjay Patel | 4ca9968 | 2017-11-27 16:37:09 +0000 | [diff] [blame] | 3412 | // Handle fcmp with constant RHS. |
| 3413 | const APFloat *C; |
| 3414 | if (match(RHS, m_APFloat(C))) { |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3415 | // Check whether the constant is an infinity. |
Sanjay Patel | 4ca9968 | 2017-11-27 16:37:09 +0000 | [diff] [blame] | 3416 | if (C->isInfinity()) { |
| 3417 | if (C->isNegative()) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3418 | switch (Pred) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3419 | case FCmpInst::FCMP_OLT: |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3420 | // No value is ordered and less than negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3421 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3422 | case FCmpInst::FCMP_UGE: |
| 3423 | // All values are unordered with or at least negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3424 | return getTrue(RetTy); |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3425 | default: |
| 3426 | break; |
| 3427 | } |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3428 | } else { |
| 3429 | switch (Pred) { |
| 3430 | case FCmpInst::FCMP_OGT: |
| 3431 | // No value is ordered and greater than infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3432 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3433 | case FCmpInst::FCMP_ULE: |
| 3434 | // All values are unordered with and at most infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3435 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3436 | default: |
| 3437 | break; |
| 3438 | } |
| 3439 | } |
| 3440 | } |
Sanjay Patel | 4ca9968 | 2017-11-27 16:37:09 +0000 | [diff] [blame] | 3441 | if (C->isZero()) { |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3442 | switch (Pred) { |
| 3443 | case FCmpInst::FCMP_UGE: |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3444 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3445 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3446 | break; |
| 3447 | case FCmpInst::FCMP_OLT: |
| 3448 | // X < 0 |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3449 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3450 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3451 | break; |
| 3452 | default: |
| 3453 | break; |
| 3454 | } |
Florian Hahn | 30932a3 | 2017-12-01 12:34:16 +0000 | [diff] [blame] | 3455 | } else if (C->isNegative()) { |
| 3456 | assert(!C->isNaN() && "Unexpected NaN constant!"); |
| 3457 | // TODO: We can catch more cases by using a range check rather than |
| 3458 | // relying on CannotBeOrderedLessThanZero. |
| 3459 | switch (Pred) { |
| 3460 | case FCmpInst::FCMP_UGE: |
| 3461 | case FCmpInst::FCMP_UGT: |
| 3462 | case FCmpInst::FCMP_UNE: |
| 3463 | // (X >= 0) implies (X > C) when (C < 0) |
| 3464 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
| 3465 | return getTrue(RetTy); |
| 3466 | break; |
| 3467 | case FCmpInst::FCMP_OEQ: |
| 3468 | case FCmpInst::FCMP_OLE: |
| 3469 | case FCmpInst::FCMP_OLT: |
| 3470 | // (X >= 0) implies !(X < C) when (C < 0) |
| 3471 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
| 3472 | return getFalse(RetTy); |
| 3473 | break; |
| 3474 | default: |
| 3475 | break; |
| 3476 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3477 | } |
| 3478 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3479 | |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3480 | // If the comparison is with the result of a select instruction, check whether |
| 3481 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3482 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3483 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3484 | return V; |
| 3485 | |
| 3486 | // If the comparison is with the result of a phi instruction, check whether |
| 3487 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3488 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3489 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3490 | return V; |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3491 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3492 | return nullptr; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3495 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3496 | FastMathFlags FMF, const SimplifyQuery &Q) { |
| 3497 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3500 | /// See if V simplifies when its operand Op is replaced with RepOp. |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3501 | static const Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3502 | const SimplifyQuery &Q, |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3503 | unsigned MaxRecurse) { |
| 3504 | // Trivial replacement. |
| 3505 | if (V == Op) |
| 3506 | return RepOp; |
| 3507 | |
Tim Northover | 997f5f1 | 2017-05-22 21:28:08 +0000 | [diff] [blame] | 3508 | // We cannot replace a constant, and shouldn't even try. |
| 3509 | if (isa<Constant>(Op)) |
| 3510 | return nullptr; |
| 3511 | |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3512 | auto *I = dyn_cast<Instruction>(V); |
| 3513 | if (!I) |
| 3514 | return nullptr; |
| 3515 | |
| 3516 | // If this is a binary operator, try to simplify it with the replaced op. |
| 3517 | if (auto *B = dyn_cast<BinaryOperator>(I)) { |
| 3518 | // Consider: |
| 3519 | // %cmp = icmp eq i32 %x, 2147483647 |
| 3520 | // %add = add nsw i32 %x, 1 |
| 3521 | // %sel = select i1 %cmp, i32 -2147483648, i32 %add |
| 3522 | // |
| 3523 | // We can't replace %sel with %add unless we strip away the flags. |
| 3524 | if (isa<OverflowingBinaryOperator>(B)) |
| 3525 | if (B->hasNoSignedWrap() || B->hasNoUnsignedWrap()) |
| 3526 | return nullptr; |
| 3527 | if (isa<PossiblyExactOperator>(B)) |
| 3528 | if (B->isExact()) |
| 3529 | return nullptr; |
| 3530 | |
| 3531 | if (MaxRecurse) { |
| 3532 | if (B->getOperand(0) == Op) |
| 3533 | return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), Q, |
| 3534 | MaxRecurse - 1); |
| 3535 | if (B->getOperand(1) == Op) |
| 3536 | return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, Q, |
| 3537 | MaxRecurse - 1); |
| 3538 | } |
| 3539 | } |
| 3540 | |
| 3541 | // Same for CmpInsts. |
| 3542 | if (CmpInst *C = dyn_cast<CmpInst>(I)) { |
| 3543 | if (MaxRecurse) { |
| 3544 | if (C->getOperand(0) == Op) |
| 3545 | return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), Q, |
| 3546 | MaxRecurse - 1); |
| 3547 | if (C->getOperand(1) == Op) |
| 3548 | return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, Q, |
| 3549 | MaxRecurse - 1); |
| 3550 | } |
| 3551 | } |
| 3552 | |
George Burgess IV | 8e807bf | 2018-04-24 00:25:01 +0000 | [diff] [blame] | 3553 | // Same for GEPs. |
| 3554 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 3555 | if (MaxRecurse) { |
| 3556 | SmallVector<Value *, 8> NewOps(GEP->getNumOperands()); |
| 3557 | transform(GEP->operands(), NewOps.begin(), |
| 3558 | [&](Value *V) { return V == Op ? RepOp : V; }); |
| 3559 | return SimplifyGEPInst(GEP->getSourceElementType(), NewOps, Q, |
| 3560 | MaxRecurse - 1); |
| 3561 | } |
| 3562 | } |
| 3563 | |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3564 | // TODO: We could hand off more cases to instsimplify here. |
| 3565 | |
| 3566 | // If all operands are constant after substituting Op for RepOp then we can |
| 3567 | // constant fold the instruction. |
| 3568 | if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) { |
| 3569 | // Build a list of all constant operands. |
| 3570 | SmallVector<Constant *, 8> ConstOps; |
| 3571 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3572 | if (I->getOperand(i) == Op) |
| 3573 | ConstOps.push_back(CRepOp); |
| 3574 | else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i))) |
| 3575 | ConstOps.push_back(COp); |
| 3576 | else |
| 3577 | break; |
| 3578 | } |
| 3579 | |
| 3580 | // All operands were constants, fold it. |
| 3581 | if (ConstOps.size() == I->getNumOperands()) { |
| 3582 | if (CmpInst *C = dyn_cast<CmpInst>(I)) |
| 3583 | return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0], |
| 3584 | ConstOps[1], Q.DL, Q.TLI); |
| 3585 | |
| 3586 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 3587 | if (!LI->isVolatile()) |
Eduard Burtescu | 1423921 | 2016-01-22 01:17:26 +0000 | [diff] [blame] | 3588 | return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3589 | |
Manuel Jacob | e902459 | 2016-01-21 06:33:22 +0000 | [diff] [blame] | 3590 | return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | return nullptr; |
| 3595 | } |
| 3596 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3597 | /// Try to simplify a select instruction when its condition operand is an |
| 3598 | /// integer comparison where one operand of the compare is a constant. |
| 3599 | static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X, |
| 3600 | const APInt *Y, bool TrueWhenUnset) { |
| 3601 | const APInt *C; |
| 3602 | |
| 3603 | // (X & Y) == 0 ? X & ~Y : X --> X |
| 3604 | // (X & Y) != 0 ? X & ~Y : X --> X & ~Y |
| 3605 | if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3606 | *Y == ~*C) |
| 3607 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3608 | |
| 3609 | // (X & Y) == 0 ? X : X & ~Y --> X & ~Y |
| 3610 | // (X & Y) != 0 ? X : X & ~Y --> X |
| 3611 | if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3612 | *Y == ~*C) |
| 3613 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3614 | |
| 3615 | if (Y->isPowerOf2()) { |
| 3616 | // (X & Y) == 0 ? X | Y : X --> X | Y |
| 3617 | // (X & Y) != 0 ? X | Y : X --> X |
| 3618 | if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3619 | *Y == *C) |
| 3620 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3621 | |
| 3622 | // (X & Y) == 0 ? X : X | Y --> X |
| 3623 | // (X & Y) != 0 ? X : X | Y --> X | Y |
| 3624 | if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3625 | *Y == *C) |
| 3626 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3627 | } |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 3628 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3629 | return nullptr; |
| 3630 | } |
| 3631 | |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3632 | /// An alternative way to test if a bit is set or not uses sgt/slt instead of |
| 3633 | /// eq/ne. |
Craig Topper | 0aa3a19 | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 3634 | static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS, |
| 3635 | ICmpInst::Predicate Pred, |
| 3636 | Value *TrueVal, Value *FalseVal) { |
| 3637 | Value *X; |
| 3638 | APInt Mask; |
| 3639 | if (!decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, X, Mask)) |
| 3640 | return nullptr; |
| 3641 | |
Craig Topper | 0aa3a19 | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 3642 | return simplifySelectBitTest(TrueVal, FalseVal, X, &Mask, |
| 3643 | Pred == ICmpInst::ICMP_EQ); |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3646 | /// Try to simplify a select instruction when its condition operand is an |
| 3647 | /// integer comparison. |
| 3648 | static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3649 | Value *FalseVal, const SimplifyQuery &Q, |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3650 | unsigned MaxRecurse) { |
| 3651 | ICmpInst::Predicate Pred; |
| 3652 | Value *CmpLHS, *CmpRHS; |
| 3653 | if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) |
| 3654 | return nullptr; |
| 3655 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3656 | if (ICmpInst::isEquality(Pred) && match(CmpRHS, m_Zero())) { |
| 3657 | Value *X; |
| 3658 | const APInt *Y; |
| 3659 | if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y)))) |
| 3660 | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y, |
| 3661 | Pred == ICmpInst::ICMP_EQ)) |
| 3662 | return V; |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
Craig Topper | 0aa3a19 | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 3665 | // Check for other compares that behave like bit test. |
| 3666 | if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, CmpRHS, Pred, |
| 3667 | TrueVal, FalseVal)) |
| 3668 | return V; |
| 3669 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3670 | // If we have an equality comparison, then we know the value in one of the |
| 3671 | // arms of the select. See if substituting this value into the arm and |
| 3672 | // simplifying the result yields the same value as the other arm. |
| 3673 | if (Pred == ICmpInst::ICMP_EQ) { |
| 3674 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3675 | TrueVal || |
| 3676 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3677 | TrueVal) |
| 3678 | return FalseVal; |
| 3679 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3680 | FalseVal || |
| 3681 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3682 | FalseVal) |
| 3683 | return FalseVal; |
| 3684 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 3685 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3686 | FalseVal || |
| 3687 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3688 | FalseVal) |
| 3689 | return TrueVal; |
| 3690 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3691 | TrueVal || |
| 3692 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3693 | TrueVal) |
| 3694 | return TrueVal; |
| 3695 | } |
| 3696 | |
| 3697 | return nullptr; |
| 3698 | } |
| 3699 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3700 | /// Given operands for a SelectInst, see if we can fold the result. |
| 3701 | /// If not, this returns null. |
Sanjay Patel | ac39520 | 2018-02-17 14:50:13 +0000 | [diff] [blame] | 3702 | static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
| 3703 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
| 3704 | if (auto *CondC = dyn_cast<Constant>(Cond)) { |
| 3705 | if (auto *TrueC = dyn_cast<Constant>(TrueVal)) |
| 3706 | if (auto *FalseC = dyn_cast<Constant>(FalseVal)) |
| 3707 | return ConstantFoldSelectInstruction(CondC, TrueC, FalseC); |
| 3708 | |
| 3709 | // select undef, X, Y -> X or Y |
| 3710 | if (isa<UndefValue>(CondC)) |
| 3711 | return isa<Constant>(FalseVal) ? FalseVal : TrueVal; |
| 3712 | |
| 3713 | // TODO: Vector constants with undef elements don't simplify. |
| 3714 | |
| 3715 | // select true, X, Y -> X |
| 3716 | if (CondC->isAllOnesValue()) |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3717 | return TrueVal; |
Sanjay Patel | ac39520 | 2018-02-17 14:50:13 +0000 | [diff] [blame] | 3718 | // select false, X, Y -> Y |
| 3719 | if (CondC->isNullValue()) |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3720 | return FalseVal; |
| 3721 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3722 | |
Sanjay Patel | ac39520 | 2018-02-17 14:50:13 +0000 | [diff] [blame] | 3723 | // select ?, X, X -> X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3724 | if (TrueVal == FalseVal) |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3725 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3726 | |
Sanjay Patel | ac39520 | 2018-02-17 14:50:13 +0000 | [diff] [blame] | 3727 | if (isa<UndefValue>(TrueVal)) // select ?, undef, X -> X |
Dan Gohman | 54664ed | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 3728 | return FalseVal; |
Sanjay Patel | ac39520 | 2018-02-17 14:50:13 +0000 | [diff] [blame] | 3729 | if (isa<UndefValue>(FalseVal)) // select ?, X, undef -> X |
Dan Gohman | 54664ed | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 3730 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3731 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3732 | if (Value *V = |
Sanjay Patel | ac39520 | 2018-02-17 14:50:13 +0000 | [diff] [blame] | 3733 | simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse)) |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3734 | return V; |
David Majnemer | c6a5e1d | 2014-11-27 06:32:46 +0000 | [diff] [blame] | 3735 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3736 | return nullptr; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3739 | Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3740 | const SimplifyQuery &Q) { |
| 3741 | return ::SimplifySelectInst(Cond, TrueVal, FalseVal, Q, RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3744 | /// Given operands for an GetElementPtrInst, see if we can fold the result. |
| 3745 | /// If not, this returns null. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3746 | static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3747 | const SimplifyQuery &Q, unsigned) { |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3748 | // The type of the GEP pointer operand. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3749 | unsigned AS = |
| 3750 | cast<PointerType>(Ops[0]->getType()->getScalarType())->getAddressSpace(); |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3751 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3752 | // getelementptr P -> P. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3753 | if (Ops.size() == 1) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3754 | return Ops[0]; |
| 3755 | |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3756 | // Compute the (pointer) type returned by the GEP instruction. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3757 | Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Ops.slice(1)); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3758 | Type *GEPTy = PointerType::get(LastType, AS); |
| 3759 | if (VectorType *VT = dyn_cast<VectorType>(Ops[0]->getType())) |
| 3760 | GEPTy = VectorType::get(GEPTy, VT->getNumElements()); |
Davide Italiano | a9f047a | 2017-04-19 14:23:42 +0000 | [diff] [blame] | 3761 | else if (VectorType *VT = dyn_cast<VectorType>(Ops[1]->getType())) |
| 3762 | GEPTy = VectorType::get(GEPTy, VT->getNumElements()); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3763 | |
| 3764 | if (isa<UndefValue>(Ops[0])) |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3765 | return UndefValue::get(GEPTy); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3766 | |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3767 | if (Ops.size() == 2) { |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3768 | // getelementptr P, 0 -> P. |
Matthew Simpson | c1c4ad6 | 2018-03-15 16:00:29 +0000 | [diff] [blame] | 3769 | if (match(Ops[1], m_Zero()) && Ops[0]->getType() == GEPTy) |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3770 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3771 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3772 | Type *Ty = SrcTy; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3773 | if (Ty->isSized()) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3774 | Value *P; |
| 3775 | uint64_t C; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3776 | uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3777 | // getelementptr P, N -> P if P points to a type of zero size. |
Matthew Simpson | c1c4ad6 | 2018-03-15 16:00:29 +0000 | [diff] [blame] | 3778 | if (TyAllocSize == 0 && Ops[0]->getType() == GEPTy) |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3779 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3780 | |
| 3781 | // The following transforms are only safe if the ptrtoint cast |
| 3782 | // doesn't truncate the pointers. |
| 3783 | if (Ops[1]->getType()->getScalarSizeInBits() == |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3784 | Q.DL.getIndexSizeInBits(AS)) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3785 | auto PtrToIntOrZero = [GEPTy](Value *P) -> Value * { |
| 3786 | if (match(P, m_Zero())) |
| 3787 | return Constant::getNullValue(GEPTy); |
| 3788 | Value *Temp; |
| 3789 | if (match(P, m_PtrToInt(m_Value(Temp)))) |
David Majnemer | 11ca297 | 2014-08-27 20:08:34 +0000 | [diff] [blame] | 3790 | if (Temp->getType() == GEPTy) |
| 3791 | return Temp; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3792 | return nullptr; |
| 3793 | }; |
| 3794 | |
| 3795 | // getelementptr V, (sub P, V) -> P if P points to a type of size 1. |
| 3796 | if (TyAllocSize == 1 && |
| 3797 | match(Ops[1], m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))))) |
| 3798 | if (Value *R = PtrToIntOrZero(P)) |
| 3799 | return R; |
| 3800 | |
| 3801 | // getelementptr V, (ashr (sub P, V), C) -> Q |
| 3802 | // if P points to a type of size 1 << C. |
| 3803 | if (match(Ops[1], |
| 3804 | m_AShr(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3805 | m_ConstantInt(C))) && |
| 3806 | TyAllocSize == 1ULL << C) |
| 3807 | if (Value *R = PtrToIntOrZero(P)) |
| 3808 | return R; |
| 3809 | |
| 3810 | // getelementptr V, (sdiv (sub P, V), C) -> Q |
| 3811 | // if P points to a type of size C. |
| 3812 | if (match(Ops[1], |
| 3813 | m_SDiv(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3814 | m_SpecificInt(TyAllocSize)))) |
| 3815 | if (Value *R = PtrToIntOrZero(P)) |
| 3816 | return R; |
| 3817 | } |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3818 | } |
| 3819 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3820 | |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3821 | if (Q.DL.getTypeAllocSize(LastType) == 1 && |
| 3822 | all_of(Ops.slice(1).drop_back(1), |
| 3823 | [](Value *Idx) { return match(Idx, m_Zero()); })) { |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3824 | unsigned IdxWidth = |
| 3825 | Q.DL.getIndexSizeInBits(Ops[0]->getType()->getPointerAddressSpace()); |
| 3826 | if (Q.DL.getTypeSizeInBits(Ops.back()->getType()) == IdxWidth) { |
| 3827 | APInt BasePtrOffset(IdxWidth, 0); |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3828 | Value *StrippedBasePtr = |
| 3829 | Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.DL, |
| 3830 | BasePtrOffset); |
| 3831 | |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3832 | // gep (gep V, C), (sub 0, V) -> C |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3833 | if (match(Ops.back(), |
| 3834 | m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr))))) { |
| 3835 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset); |
| 3836 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3837 | } |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3838 | // gep (gep V, C), (xor V, -1) -> C-1 |
| 3839 | if (match(Ops.back(), |
| 3840 | m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes()))) { |
| 3841 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1); |
| 3842 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3843 | } |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3844 | } |
| 3845 | } |
| 3846 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3847 | // Check to see if this is constant foldable. |
Craig Topper | da8037f | 2017-06-04 22:41:56 +0000 | [diff] [blame] | 3848 | if (!all_of(Ops, [](Value *V) { return isa<Constant>(V); })) |
| 3849 | return nullptr; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3850 | |
Joey Gouly | 61eaa63 | 2017-06-06 10:17:14 +0000 | [diff] [blame] | 3851 | auto *CE = ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ops[0]), |
| 3852 | Ops.slice(1)); |
| 3853 | if (auto *CEFolded = ConstantFoldConstant(CE, Q.DL)) |
| 3854 | return CEFolded; |
| 3855 | return CE; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 3858 | Value *llvm::SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3859 | const SimplifyQuery &Q) { |
| 3860 | return ::SimplifyGEPInst(SrcTy, Ops, Q, RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3863 | /// Given operands for an InsertValueInst, see if we can fold the result. |
| 3864 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3865 | static Value *SimplifyInsertValueInst(Value *Agg, Value *Val, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3866 | ArrayRef<unsigned> Idxs, const SimplifyQuery &Q, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3867 | unsigned) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3868 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 3869 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 3870 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 3871 | |
| 3872 | // insertvalue x, undef, n -> x |
| 3873 | if (match(Val, m_Undef())) |
| 3874 | return Agg; |
| 3875 | |
| 3876 | // insertvalue x, (extractvalue y, n), n |
| 3877 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | 4b79c21 | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 3878 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 3879 | EV->getIndices() == Idxs) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3880 | // insertvalue undef, (extractvalue y, n), n -> y |
| 3881 | if (match(Agg, m_Undef())) |
| 3882 | return EV->getAggregateOperand(); |
| 3883 | |
| 3884 | // insertvalue y, (extractvalue y, n), n -> y |
| 3885 | if (Agg == EV->getAggregateOperand()) |
| 3886 | return Agg; |
| 3887 | } |
| 3888 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3889 | return nullptr; |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3892 | Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 3893 | ArrayRef<unsigned> Idxs, |
| 3894 | const SimplifyQuery &Q) { |
| 3895 | return ::SimplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit); |
| 3896 | } |
| 3897 | |
Igor Laevsky | e0edb66 | 2017-12-13 11:21:18 +0000 | [diff] [blame] | 3898 | Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx, |
| 3899 | const SimplifyQuery &Q) { |
| 3900 | // Try to constant fold. |
| 3901 | auto *VecC = dyn_cast<Constant>(Vec); |
| 3902 | auto *ValC = dyn_cast<Constant>(Val); |
| 3903 | auto *IdxC = dyn_cast<Constant>(Idx); |
| 3904 | if (VecC && ValC && IdxC) |
| 3905 | return ConstantFoldInsertElementInstruction(VecC, ValC, IdxC); |
| 3906 | |
| 3907 | // Fold into undef if index is out of bounds. |
| 3908 | if (auto *CI = dyn_cast<ConstantInt>(Idx)) { |
| 3909 | uint64_t NumElements = cast<VectorType>(Vec->getType())->getNumElements(); |
Igor Laevsky | e0edb66 | 2017-12-13 11:21:18 +0000 | [diff] [blame] | 3910 | if (CI->uge(NumElements)) |
| 3911 | return UndefValue::get(Vec->getType()); |
| 3912 | } |
| 3913 | |
Philip Reames | e499bc3 | 2017-12-30 05:54:22 +0000 | [diff] [blame] | 3914 | // If index is undef, it might be out of bounds (see above case) |
| 3915 | if (isa<UndefValue>(Idx)) |
| 3916 | return UndefValue::get(Vec->getType()); |
Igor Laevsky | e0edb66 | 2017-12-13 11:21:18 +0000 | [diff] [blame] | 3917 | |
| 3918 | return nullptr; |
| 3919 | } |
| 3920 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3921 | /// Given operands for an ExtractValueInst, see if we can fold the result. |
| 3922 | /// If not, this returns null. |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3923 | static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3924 | const SimplifyQuery &, unsigned) { |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3925 | if (auto *CAgg = dyn_cast<Constant>(Agg)) |
| 3926 | return ConstantFoldExtractValueInstruction(CAgg, Idxs); |
| 3927 | |
| 3928 | // extractvalue x, (insertvalue y, elt, n), n -> elt |
| 3929 | unsigned NumIdxs = Idxs.size(); |
| 3930 | for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr; |
| 3931 | IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) { |
| 3932 | ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices(); |
| 3933 | unsigned NumInsertValueIdxs = InsertValueIdxs.size(); |
| 3934 | unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs); |
| 3935 | if (InsertValueIdxs.slice(0, NumCommonIdxs) == |
| 3936 | Idxs.slice(0, NumCommonIdxs)) { |
| 3937 | if (NumIdxs == NumInsertValueIdxs) |
| 3938 | return IVI->getInsertedValueOperand(); |
| 3939 | break; |
| 3940 | } |
| 3941 | } |
| 3942 | |
| 3943 | return nullptr; |
| 3944 | } |
| 3945 | |
| 3946 | Value *llvm::SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3947 | const SimplifyQuery &Q) { |
| 3948 | return ::SimplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit); |
| 3949 | } |
| 3950 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3951 | /// Given operands for an ExtractElementInst, see if we can fold the result. |
| 3952 | /// If not, this returns null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3953 | static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &, |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3954 | unsigned) { |
| 3955 | if (auto *CVec = dyn_cast<Constant>(Vec)) { |
| 3956 | if (auto *CIdx = dyn_cast<Constant>(Idx)) |
| 3957 | return ConstantFoldExtractElementInstruction(CVec, CIdx); |
| 3958 | |
| 3959 | // The index is not relevant if our vector is a splat. |
| 3960 | if (auto *Splat = CVec->getSplatValue()) |
| 3961 | return Splat; |
| 3962 | |
| 3963 | if (isa<UndefValue>(Vec)) |
| 3964 | return UndefValue::get(Vec->getType()->getVectorElementType()); |
| 3965 | } |
| 3966 | |
| 3967 | // If extracting a specified index from the vector, see if we can recursively |
| 3968 | // find a previously computed scalar that was inserted into the vector. |
Philip Reames | e499bc3 | 2017-12-30 05:54:22 +0000 | [diff] [blame] | 3969 | if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) { |
| 3970 | if (IdxC->getValue().uge(Vec->getType()->getVectorNumElements())) |
| 3971 | // definitely out of bounds, thus undefined result |
| 3972 | return UndefValue::get(Vec->getType()->getVectorElementType()); |
| 3973 | if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue())) |
| 3974 | return Elt; |
| 3975 | } |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3976 | |
Zvi Rackover | 2e6e88f | 2017-12-06 17:51:46 +0000 | [diff] [blame] | 3977 | // An undef extract index can be arbitrarily chosen to be an out-of-range |
| 3978 | // index value, which would result in the instruction being undef. |
| 3979 | if (isa<UndefValue>(Idx)) |
| 3980 | return UndefValue::get(Vec->getType()->getVectorElementType()); |
| 3981 | |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3982 | return nullptr; |
| 3983 | } |
| 3984 | |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3985 | Value *llvm::SimplifyExtractElementInst(Value *Vec, Value *Idx, |
| 3986 | const SimplifyQuery &Q) { |
| 3987 | return ::SimplifyExtractElementInst(Vec, Idx, Q, RecursionLimit); |
| 3988 | } |
| 3989 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3990 | /// See if we can fold the given phi. If not, returns null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 3991 | static Value *SimplifyPHINode(PHINode *PN, const SimplifyQuery &Q) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3992 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 3993 | // with the common value. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3994 | Value *CommonValue = nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3995 | bool HasUndefInput = false; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 3996 | for (Value *Incoming : PN->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3997 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 3998 | if (Incoming == PN) continue; |
| 3999 | if (isa<UndefValue>(Incoming)) { |
| 4000 | // Remember that we saw an undef value, but otherwise ignore them. |
| 4001 | HasUndefInput = true; |
| 4002 | continue; |
| 4003 | } |
| 4004 | if (CommonValue && Incoming != CommonValue) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4005 | return nullptr; // Not the same, bail out. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4006 | CommonValue = Incoming; |
| 4007 | } |
| 4008 | |
| 4009 | // If CommonValue is null then all of the incoming values were either undef or |
| 4010 | // equal to the phi node itself. |
| 4011 | if (!CommonValue) |
| 4012 | return UndefValue::get(PN->getType()); |
| 4013 | |
| 4014 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 4015 | // instruction, we cannot return X as the result of the PHI node unless it |
| 4016 | // dominates the PHI block. |
| 4017 | if (HasUndefInput) |
Sanjay Patel | 5da361a | 2018-04-10 18:38:19 +0000 | [diff] [blame] | 4018 | return valueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4019 | |
| 4020 | return CommonValue; |
| 4021 | } |
| 4022 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4023 | static Value *SimplifyCastInst(unsigned CastOpc, Value *Op, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4024 | Type *Ty, const SimplifyQuery &Q, unsigned MaxRecurse) { |
David Majnemer | 126de5d | 2016-07-25 03:39:21 +0000 | [diff] [blame] | 4025 | if (auto *C = dyn_cast<Constant>(Op)) |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4026 | return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 4027 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4028 | if (auto *CI = dyn_cast<CastInst>(Op)) { |
| 4029 | auto *Src = CI->getOperand(0); |
| 4030 | Type *SrcTy = Src->getType(); |
| 4031 | Type *MidTy = CI->getType(); |
| 4032 | Type *DstTy = Ty; |
| 4033 | if (Src->getType() == Ty) { |
| 4034 | auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode()); |
| 4035 | auto SecondOp = static_cast<Instruction::CastOps>(CastOpc); |
| 4036 | Type *SrcIntPtrTy = |
| 4037 | SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr; |
| 4038 | Type *MidIntPtrTy = |
| 4039 | MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr; |
| 4040 | Type *DstIntPtrTy = |
| 4041 | DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr; |
| 4042 | if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy, |
| 4043 | SrcIntPtrTy, MidIntPtrTy, |
| 4044 | DstIntPtrTy) == Instruction::BitCast) |
| 4045 | return Src; |
| 4046 | } |
| 4047 | } |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4048 | |
| 4049 | // bitcast x -> x |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4050 | if (CastOpc == Instruction::BitCast) |
| 4051 | if (Op->getType() == Ty) |
| 4052 | return Op; |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4053 | |
| 4054 | return nullptr; |
| 4055 | } |
| 4056 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4057 | Value *llvm::SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4058 | const SimplifyQuery &Q) { |
| 4059 | return ::SimplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit); |
| 4060 | } |
| 4061 | |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4062 | /// For the given destination element of a shuffle, peek through shuffles to |
| 4063 | /// match a root vector source operand that contains that element in the same |
| 4064 | /// vector lane (ie, the same mask index), so we can eliminate the shuffle(s). |
| 4065 | static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1, |
Zvi Rackover | 558f86b | 2017-05-08 15:46:58 +0000 | [diff] [blame] | 4066 | int MaskVal, Value *RootVec, |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4067 | unsigned MaxRecurse) { |
| 4068 | if (!MaxRecurse--) |
| 4069 | return nullptr; |
| 4070 | |
| 4071 | // Bail out if any mask value is undefined. That kind of shuffle may be |
| 4072 | // simplified further based on demanded bits or other folds. |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4073 | if (MaskVal == -1) |
| 4074 | return nullptr; |
| 4075 | |
| 4076 | // The mask value chooses which source operand we need to look at next. |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4077 | int InVecNumElts = Op0->getType()->getVectorNumElements(); |
Zvi Rackover | 558f86b | 2017-05-08 15:46:58 +0000 | [diff] [blame] | 4078 | int RootElt = MaskVal; |
| 4079 | Value *SourceOp = Op0; |
| 4080 | if (MaskVal >= InVecNumElts) { |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4081 | RootElt = MaskVal - InVecNumElts; |
| 4082 | SourceOp = Op1; |
| 4083 | } |
| 4084 | |
| 4085 | // If the source operand is a shuffle itself, look through it to find the |
| 4086 | // matching root vector. |
| 4087 | if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) { |
| 4088 | return foldIdentityShuffles( |
| 4089 | DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1), |
Zvi Rackover | 558f86b | 2017-05-08 15:46:58 +0000 | [diff] [blame] | 4090 | SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse); |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | // TODO: Look through bitcasts? What if the bitcast changes the vector element |
| 4094 | // size? |
| 4095 | |
| 4096 | // The source operand is not a shuffle. Initialize the root vector value for |
| 4097 | // this shuffle if that has not been done yet. |
| 4098 | if (!RootVec) |
| 4099 | RootVec = SourceOp; |
| 4100 | |
| 4101 | // Give up as soon as a source operand does not match the existing root value. |
| 4102 | if (RootVec != SourceOp) |
| 4103 | return nullptr; |
| 4104 | |
| 4105 | // The element must be coming from the same lane in the source vector |
| 4106 | // (although it may have crossed lanes in intermediate shuffles). |
| 4107 | if (RootElt != DestElt) |
| 4108 | return nullptr; |
| 4109 | |
| 4110 | return RootVec; |
| 4111 | } |
| 4112 | |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4113 | static Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4114 | Type *RetTy, const SimplifyQuery &Q, |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4115 | unsigned MaxRecurse) { |
Zvi Rackover | 4086e13 | 2017-04-30 06:06:26 +0000 | [diff] [blame] | 4116 | if (isa<UndefValue>(Mask)) |
| 4117 | return UndefValue::get(RetTy); |
| 4118 | |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame] | 4119 | Type *InVecTy = Op0->getType(); |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4120 | unsigned MaskNumElts = Mask->getType()->getVectorNumElements(); |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame] | 4121 | unsigned InVecNumElts = InVecTy->getVectorNumElements(); |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4122 | |
Zvi Rackover | 0411e46 | 2017-04-30 06:10:54 +0000 | [diff] [blame] | 4123 | SmallVector<int, 32> Indices; |
| 4124 | ShuffleVectorInst::getShuffleMask(Mask, Indices); |
| 4125 | assert(MaskNumElts == Indices.size() && |
| 4126 | "Size of Indices not same as number of mask elements?"); |
| 4127 | |
Zvi Rackover | 973ff7c | 2017-05-07 18:16:37 +0000 | [diff] [blame] | 4128 | // Canonicalization: If mask does not select elements from an input vector, |
| 4129 | // replace that input vector with undef. |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4130 | bool MaskSelects0 = false, MaskSelects1 = false; |
| 4131 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Zvi Rackover | 0411e46 | 2017-04-30 06:10:54 +0000 | [diff] [blame] | 4132 | if (Indices[i] == -1) |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4133 | continue; |
Zvi Rackover | 0411e46 | 2017-04-30 06:10:54 +0000 | [diff] [blame] | 4134 | if ((unsigned)Indices[i] < InVecNumElts) |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4135 | MaskSelects0 = true; |
| 4136 | else |
| 4137 | MaskSelects1 = true; |
| 4138 | } |
Zvi Rackover | 973ff7c | 2017-05-07 18:16:37 +0000 | [diff] [blame] | 4139 | if (!MaskSelects0) |
| 4140 | Op0 = UndefValue::get(InVecTy); |
| 4141 | if (!MaskSelects1) |
| 4142 | Op1 = UndefValue::get(InVecTy); |
| 4143 | |
| 4144 | auto *Op0Const = dyn_cast<Constant>(Op0); |
| 4145 | auto *Op1Const = dyn_cast<Constant>(Op1); |
| 4146 | |
| 4147 | // If all operands are constant, constant fold the shuffle. |
| 4148 | if (Op0Const && Op1Const) |
| 4149 | return ConstantFoldShuffleVectorInstruction(Op0Const, Op1Const, Mask); |
| 4150 | |
| 4151 | // Canonicalization: if only one input vector is constant, it shall be the |
| 4152 | // second one. |
| 4153 | if (Op0Const && !Op1Const) { |
| 4154 | std::swap(Op0, Op1); |
Zvi Rackover | dfbd3d7 | 2017-05-08 12:40:18 +0000 | [diff] [blame] | 4155 | ShuffleVectorInst::commuteShuffleMask(Indices, InVecNumElts); |
Zvi Rackover | 973ff7c | 2017-05-07 18:16:37 +0000 | [diff] [blame] | 4156 | } |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame] | 4157 | |
| 4158 | // A shuffle of a splat is always the splat itself. Legal if the shuffle's |
| 4159 | // value type is same as the input vectors' type. |
| 4160 | if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0)) |
Zvi Rackover | 973ff7c | 2017-05-07 18:16:37 +0000 | [diff] [blame] | 4161 | if (isa<UndefValue>(Op1) && RetTy == InVecTy && |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame] | 4162 | OpShuf->getMask()->getSplatValue()) |
| 4163 | return Op0; |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4164 | |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4165 | // Don't fold a shuffle with undef mask elements. This may get folded in a |
| 4166 | // better way using demanded bits or other analysis. |
| 4167 | // TODO: Should we allow this? |
Zvi Rackover | 0411e46 | 2017-04-30 06:10:54 +0000 | [diff] [blame] | 4168 | if (find(Indices, -1) != Indices.end()) |
| 4169 | return nullptr; |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4170 | |
| 4171 | // Check if every element of this shuffle can be mapped back to the |
| 4172 | // corresponding element of a single root vector. If so, we don't need this |
| 4173 | // shuffle. This handles simple identity shuffles as well as chains of |
| 4174 | // shuffles that may widen/narrow and/or move elements across lanes and back. |
| 4175 | Value *RootVec = nullptr; |
| 4176 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
| 4177 | // Note that recursion is limited for each vector element, so if any element |
| 4178 | // exceeds the limit, this will fail to simplify. |
Zvi Rackover | 558f86b | 2017-05-08 15:46:58 +0000 | [diff] [blame] | 4179 | RootVec = |
| 4180 | foldIdentityShuffles(i, Op0, Op1, Indices[i], RootVec, MaxRecurse); |
Sanjay Patel | a3c297d | 2017-04-19 16:48:22 +0000 | [diff] [blame] | 4181 | |
| 4182 | // We can't replace a widening/narrowing shuffle with one of its operands. |
| 4183 | if (!RootVec || RootVec->getType() != RetTy) |
| 4184 | return nullptr; |
| 4185 | } |
| 4186 | return RootVec; |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | /// Given operands for a ShuffleVectorInst, fold the result or return null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4190 | Value *llvm::SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, |
| 4191 | Type *RetTy, const SimplifyQuery &Q) { |
| 4192 | return ::SimplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit); |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
Sanjay Patel | e235942 | 2018-03-21 19:31:53 +0000 | [diff] [blame] | 4195 | static Constant *propagateNaN(Constant *In) { |
| 4196 | // If the input is a vector with undef elements, just return a default NaN. |
| 4197 | if (!In->isNaN()) |
| 4198 | return ConstantFP::getNaN(In->getType()); |
| 4199 | |
| 4200 | // Propagate the existing NaN constant when possible. |
| 4201 | // TODO: Should we quiet a signaling NaN? |
| 4202 | return In; |
| 4203 | } |
| 4204 | |
| 4205 | static Constant *simplifyFPBinop(Value *Op0, Value *Op1) { |
| 4206 | if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1)) |
| 4207 | return ConstantFP::getNaN(Op0->getType()); |
| 4208 | |
| 4209 | if (match(Op0, m_NaN())) |
| 4210 | return propagateNaN(cast<Constant>(Op0)); |
| 4211 | if (match(Op1, m_NaN())) |
| 4212 | return propagateNaN(cast<Constant>(Op1)); |
| 4213 | |
| 4214 | return nullptr; |
| 4215 | } |
| 4216 | |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4217 | /// Given operands for an FAdd, see if we can fold the result. If not, this |
| 4218 | /// returns null. |
| 4219 | static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4220 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
| 4221 | if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q)) |
| 4222 | return C; |
| 4223 | |
Sanjay Patel | e235942 | 2018-03-21 19:31:53 +0000 | [diff] [blame] | 4224 | if (Constant *C = simplifyFPBinop(Op0, Op1)) |
| 4225 | return C; |
Sanjay Patel | 4222716 | 2018-03-10 16:51:28 +0000 | [diff] [blame] | 4226 | |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4227 | // fadd X, -0 ==> X |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 4228 | if (match(Op1, m_NegZeroFP())) |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4229 | return Op0; |
| 4230 | |
| 4231 | // fadd X, 0 ==> X, when we know X is not -0 |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 4232 | if (match(Op1, m_PosZeroFP()) && |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4233 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
| 4234 | return Op0; |
| 4235 | |
Sanjay Patel | 11f7f99 | 2018-03-14 21:23:27 +0000 | [diff] [blame] | 4236 | // With nnan: (+/-0.0 - X) + X --> 0.0 (and commuted variant) |
| 4237 | // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN. |
| 4238 | // Negative zeros are allowed because we always end up with positive zero: |
| 4239 | // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0 |
| 4240 | // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0 |
| 4241 | // X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0 |
| 4242 | // X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0 |
Sanjay Patel | a4f42f2 | 2018-03-15 14:29:27 +0000 | [diff] [blame] | 4243 | if (FMF.noNaNs() && (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) || |
| 4244 | match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))) |
Sanjay Patel | 11f7f99 | 2018-03-14 21:23:27 +0000 | [diff] [blame] | 4245 | return ConstantFP::getNullValue(Op0->getType()); |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4246 | |
| 4247 | return nullptr; |
| 4248 | } |
| 4249 | |
| 4250 | /// Given operands for an FSub, see if we can fold the result. If not, this |
| 4251 | /// returns null. |
| 4252 | static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4253 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
| 4254 | if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q)) |
| 4255 | return C; |
| 4256 | |
Sanjay Patel | e235942 | 2018-03-21 19:31:53 +0000 | [diff] [blame] | 4257 | if (Constant *C = simplifyFPBinop(Op0, Op1)) |
| 4258 | return C; |
Sanjay Patel | 4222716 | 2018-03-10 16:51:28 +0000 | [diff] [blame] | 4259 | |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 4260 | // fsub X, +0 ==> X |
| 4261 | if (match(Op1, m_PosZeroFP())) |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4262 | return Op0; |
| 4263 | |
| 4264 | // fsub X, -0 ==> X, when we know X is not -0 |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 4265 | if (match(Op1, m_NegZeroFP()) && |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4266 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
| 4267 | return Op0; |
| 4268 | |
| 4269 | // fsub -0.0, (fsub -0.0, X) ==> X |
| 4270 | Value *X; |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 4271 | if (match(Op0, m_NegZeroFP()) && |
| 4272 | match(Op1, m_FSub(m_NegZeroFP(), m_Value(X)))) |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4273 | return X; |
| 4274 | |
| 4275 | // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored. |
Sanjay Patel | a4f42f2 | 2018-03-15 14:29:27 +0000 | [diff] [blame] | 4276 | if (FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()) && |
| 4277 | match(Op1, m_FSub(m_AnyZeroFP(), m_Value(X)))) |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4278 | return X; |
| 4279 | |
| 4280 | // fsub nnan x, x ==> 0.0 |
| 4281 | if (FMF.noNaNs() && Op0 == Op1) |
| 4282 | return Constant::getNullValue(Op0->getType()); |
| 4283 | |
| 4284 | return nullptr; |
| 4285 | } |
| 4286 | |
| 4287 | /// Given the operands for an FMul, see if we can fold the result |
| 4288 | static Value *SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4289 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
| 4290 | if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q)) |
| 4291 | return C; |
| 4292 | |
Sanjay Patel | e235942 | 2018-03-21 19:31:53 +0000 | [diff] [blame] | 4293 | if (Constant *C = simplifyFPBinop(Op0, Op1)) |
| 4294 | return C; |
Sanjay Patel | 4222716 | 2018-03-10 16:51:28 +0000 | [diff] [blame] | 4295 | |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4296 | // fmul X, 1.0 ==> X |
| 4297 | if (match(Op1, m_FPOne())) |
| 4298 | return Op0; |
| 4299 | |
| 4300 | // fmul nnan nsz X, 0 ==> 0 |
Sanjay Patel | a4f42f2 | 2018-03-15 14:29:27 +0000 | [diff] [blame] | 4301 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZeroFP())) |
| 4302 | return ConstantFP::getNullValue(Op0->getType()); |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4303 | |
Sanjay Patel | 95ec4a4 | 2018-03-18 14:12:25 +0000 | [diff] [blame] | 4304 | // sqrt(X) * sqrt(X) --> X, if we can: |
| 4305 | // 1. Remove the intermediate rounding (reassociate). |
| 4306 | // 2. Ignore non-zero negative numbers because sqrt would produce NAN. |
| 4307 | // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0. |
Sanjay Patel | db53d18 | 2018-02-23 22:20:13 +0000 | [diff] [blame] | 4308 | Value *X; |
Sanjay Patel | 95ec4a4 | 2018-03-18 14:12:25 +0000 | [diff] [blame] | 4309 | if (Op0 == Op1 && match(Op0, m_Intrinsic<Intrinsic::sqrt>(m_Value(X))) && |
| 4310 | FMF.allowReassoc() && FMF.noNaNs() && FMF.noSignedZeros()) |
Sanjay Patel | db53d18 | 2018-02-23 22:20:13 +0000 | [diff] [blame] | 4311 | return X; |
| 4312 | |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4313 | return nullptr; |
| 4314 | } |
| 4315 | |
| 4316 | Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4317 | const SimplifyQuery &Q) { |
| 4318 | return ::SimplifyFAddInst(Op0, Op1, FMF, Q, RecursionLimit); |
| 4319 | } |
| 4320 | |
| 4321 | |
| 4322 | Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4323 | const SimplifyQuery &Q) { |
| 4324 | return ::SimplifyFSubInst(Op0, Op1, FMF, Q, RecursionLimit); |
| 4325 | } |
| 4326 | |
| 4327 | Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4328 | const SimplifyQuery &Q) { |
| 4329 | return ::SimplifyFMulInst(Op0, Op1, FMF, Q, RecursionLimit); |
| 4330 | } |
| 4331 | |
| 4332 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4333 | const SimplifyQuery &Q, unsigned) { |
| 4334 | if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q)) |
| 4335 | return C; |
| 4336 | |
Sanjay Patel | e235942 | 2018-03-21 19:31:53 +0000 | [diff] [blame] | 4337 | if (Constant *C = simplifyFPBinop(Op0, Op1)) |
| 4338 | return C; |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4339 | |
| 4340 | // X / 1.0 -> X |
| 4341 | if (match(Op1, m_FPOne())) |
| 4342 | return Op0; |
| 4343 | |
| 4344 | // 0 / X -> 0 |
| 4345 | // Requires that NaNs are off (X could be zero) and signed zeroes are |
| 4346 | // ignored (X could be positive or negative, so the output sign is unknown). |
Sanjay Patel | a4f42f2 | 2018-03-15 14:29:27 +0000 | [diff] [blame] | 4347 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP())) |
| 4348 | return ConstantFP::getNullValue(Op0->getType()); |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4349 | |
| 4350 | if (FMF.noNaNs()) { |
| 4351 | // X / X -> 1.0 is legal when NaNs are ignored. |
Sanjay Patel | 83f0566 | 2018-01-30 00:18:37 +0000 | [diff] [blame] | 4352 | // We can ignore infinities because INF/INF is NaN. |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4353 | if (Op0 == Op1) |
| 4354 | return ConstantFP::get(Op0->getType(), 1.0); |
| 4355 | |
Sanjay Patel | 83f0566 | 2018-01-30 00:18:37 +0000 | [diff] [blame] | 4356 | // (X * Y) / Y --> X if we can reassociate to the above form. |
| 4357 | Value *X; |
| 4358 | if (FMF.allowReassoc() && match(Op0, m_c_FMul(m_Value(X), m_Specific(Op1)))) |
| 4359 | return X; |
| 4360 | |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4361 | // -X / X -> -1.0 and |
| 4362 | // X / -X -> -1.0 are legal when NaNs are ignored. |
| 4363 | // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored. |
| 4364 | if ((BinaryOperator::isFNeg(Op0, /*IgnoreZeroSign=*/true) && |
| 4365 | BinaryOperator::getFNegArgument(Op0) == Op1) || |
| 4366 | (BinaryOperator::isFNeg(Op1, /*IgnoreZeroSign=*/true) && |
| 4367 | BinaryOperator::getFNegArgument(Op1) == Op0)) |
| 4368 | return ConstantFP::get(Op0->getType(), -1.0); |
| 4369 | } |
| 4370 | |
| 4371 | return nullptr; |
| 4372 | } |
| 4373 | |
| 4374 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4375 | const SimplifyQuery &Q) { |
| 4376 | return ::SimplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit); |
| 4377 | } |
| 4378 | |
| 4379 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4380 | const SimplifyQuery &Q, unsigned) { |
| 4381 | if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q)) |
| 4382 | return C; |
| 4383 | |
Sanjay Patel | e235942 | 2018-03-21 19:31:53 +0000 | [diff] [blame] | 4384 | if (Constant *C = simplifyFPBinop(Op0, Op1)) |
| 4385 | return C; |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4386 | |
Sanjay Patel | 8f063d0 | 2018-03-15 14:04:31 +0000 | [diff] [blame] | 4387 | // Unlike fdiv, the result of frem always matches the sign of the dividend. |
| 4388 | // The constant match may include undef elements in a vector, so return a full |
| 4389 | // zero constant as the result. |
| 4390 | if (FMF.noNaNs()) { |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 4391 | // +0 % X -> 0 |
| 4392 | if (match(Op0, m_PosZeroFP())) |
Sanjay Patel | 8f063d0 | 2018-03-15 14:04:31 +0000 | [diff] [blame] | 4393 | return ConstantFP::getNullValue(Op0->getType()); |
| 4394 | // -0 % X -> -0 |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 4395 | if (match(Op0, m_NegZeroFP())) |
Sanjay Patel | 8f063d0 | 2018-03-15 14:04:31 +0000 | [diff] [blame] | 4396 | return ConstantFP::getNegativeZero(Op0->getType()); |
| 4397 | } |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4398 | |
| 4399 | return nullptr; |
| 4400 | } |
| 4401 | |
| 4402 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 4403 | const SimplifyQuery &Q) { |
| 4404 | return ::SimplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit); |
| 4405 | } |
| 4406 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4407 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4408 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4409 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 4410 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4411 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4412 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4413 | switch (Opcode) { |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4414 | case Instruction::Add: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4415 | return SimplifyAddInst(LHS, RHS, false, false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4416 | case Instruction::Sub: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4417 | return SimplifySubInst(LHS, RHS, false, false, Q, MaxRecurse); |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4418 | case Instruction::Mul: |
| 4419 | return SimplifyMulInst(LHS, RHS, Q, MaxRecurse); |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4420 | case Instruction::SDiv: |
| 4421 | return SimplifySDivInst(LHS, RHS, Q, MaxRecurse); |
| 4422 | case Instruction::UDiv: |
| 4423 | return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse); |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4424 | case Instruction::SRem: |
| 4425 | return SimplifySRemInst(LHS, RHS, Q, MaxRecurse); |
| 4426 | case Instruction::URem: |
| 4427 | return SimplifyURemInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4428 | case Instruction::Shl: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4429 | return SimplifyShlInst(LHS, RHS, false, false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4430 | case Instruction::LShr: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4431 | return SimplifyLShrInst(LHS, RHS, false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4432 | case Instruction::AShr: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4433 | return SimplifyAShrInst(LHS, RHS, false, Q, MaxRecurse); |
| 4434 | case Instruction::And: |
| 4435 | return SimplifyAndInst(LHS, RHS, Q, MaxRecurse); |
| 4436 | case Instruction::Or: |
| 4437 | return SimplifyOrInst(LHS, RHS, Q, MaxRecurse); |
| 4438 | case Instruction::Xor: |
| 4439 | return SimplifyXorInst(LHS, RHS, Q, MaxRecurse); |
Sanjay Patel | fa877fd | 2017-09-11 13:34:27 +0000 | [diff] [blame] | 4440 | case Instruction::FAdd: |
| 4441 | return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4442 | case Instruction::FSub: |
| 4443 | return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4444 | case Instruction::FMul: |
| 4445 | return SimplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4446 | case Instruction::FDiv: |
| 4447 | return SimplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4448 | case Instruction::FRem: |
| 4449 | return SimplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4450 | default: |
Craig Topper | 8ef20ea | 2017-04-06 18:59:08 +0000 | [diff] [blame] | 4451 | llvm_unreachable("Unexpected opcode"); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4452 | } |
| 4453 | } |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4454 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4455 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 4456 | /// If not, this returns null. |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4457 | /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the |
| 4458 | /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp. |
| 4459 | static Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4460 | const FastMathFlags &FMF, const SimplifyQuery &Q, |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4461 | unsigned MaxRecurse) { |
| 4462 | switch (Opcode) { |
| 4463 | case Instruction::FAdd: |
| 4464 | return SimplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 4465 | case Instruction::FSub: |
| 4466 | return SimplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 4467 | case Instruction::FMul: |
| 4468 | return SimplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse); |
Zia Ansari | 394cef8 | 2016-12-08 23:27:40 +0000 | [diff] [blame] | 4469 | case Instruction::FDiv: |
| 4470 | return SimplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4471 | default: |
| 4472 | return SimplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse); |
| 4473 | } |
| 4474 | } |
| 4475 | |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4476 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4477 | const SimplifyQuery &Q) { |
| 4478 | return ::SimplifyBinOp(Opcode, LHS, RHS, Q, RecursionLimit); |
| 4479 | } |
| 4480 | |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4481 | Value *llvm::SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Daniel Berlin | e8d74dc | 2017-04-26 04:10:00 +0000 | [diff] [blame] | 4482 | FastMathFlags FMF, const SimplifyQuery &Q) { |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4483 | return ::SimplifyFPBinOp(Opcode, LHS, RHS, FMF, Q, RecursionLimit); |
| 4484 | } |
| 4485 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4486 | /// Given operands for a CmpInst, see if we can fold the result. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4487 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4488 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4489 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4490 | return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 4491 | return SimplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4495 | const SimplifyQuery &Q) { |
| 4496 | return ::SimplifyCmpInst(Predicate, LHS, RHS, Q, RecursionLimit); |
| 4497 | } |
| 4498 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4499 | static bool IsIdempotent(Intrinsic::ID ID) { |
| 4500 | switch (ID) { |
| 4501 | default: return false; |
| 4502 | |
| 4503 | // Unary idempotent: f(f(x)) = f(x) |
| 4504 | case Intrinsic::fabs: |
| 4505 | case Intrinsic::floor: |
| 4506 | case Intrinsic::ceil: |
| 4507 | case Intrinsic::trunc: |
| 4508 | case Intrinsic::rint: |
| 4509 | case Intrinsic::nearbyint: |
Hal Finkel | 171817e | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 4510 | case Intrinsic::round: |
Matt Arsenault | 3ced3d9 | 2017-09-07 01:21:43 +0000 | [diff] [blame] | 4511 | case Intrinsic::canonicalize: |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4512 | return true; |
| 4513 | } |
| 4514 | } |
| 4515 | |
Peter Collingbourne | 7dd8dbf | 2016-04-22 21:18:02 +0000 | [diff] [blame] | 4516 | static Value *SimplifyRelativeLoad(Constant *Ptr, Constant *Offset, |
| 4517 | const DataLayout &DL) { |
| 4518 | GlobalValue *PtrSym; |
| 4519 | APInt PtrOffset; |
| 4520 | if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL)) |
| 4521 | return nullptr; |
| 4522 | |
| 4523 | Type *Int8PtrTy = Type::getInt8PtrTy(Ptr->getContext()); |
| 4524 | Type *Int32Ty = Type::getInt32Ty(Ptr->getContext()); |
| 4525 | Type *Int32PtrTy = Int32Ty->getPointerTo(); |
| 4526 | Type *Int64Ty = Type::getInt64Ty(Ptr->getContext()); |
| 4527 | |
| 4528 | auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset); |
| 4529 | if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64) |
| 4530 | return nullptr; |
| 4531 | |
| 4532 | uint64_t OffsetInt = OffsetConstInt->getSExtValue(); |
| 4533 | if (OffsetInt % 4 != 0) |
| 4534 | return nullptr; |
| 4535 | |
| 4536 | Constant *C = ConstantExpr::getGetElementPtr( |
| 4537 | Int32Ty, ConstantExpr::getBitCast(Ptr, Int32PtrTy), |
| 4538 | ConstantInt::get(Int64Ty, OffsetInt / 4)); |
| 4539 | Constant *Loaded = ConstantFoldLoadFromConstPtr(C, Int32Ty, DL); |
| 4540 | if (!Loaded) |
| 4541 | return nullptr; |
| 4542 | |
| 4543 | auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded); |
| 4544 | if (!LoadedCE) |
| 4545 | return nullptr; |
| 4546 | |
| 4547 | if (LoadedCE->getOpcode() == Instruction::Trunc) { |
| 4548 | LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4549 | if (!LoadedCE) |
| 4550 | return nullptr; |
| 4551 | } |
| 4552 | |
| 4553 | if (LoadedCE->getOpcode() != Instruction::Sub) |
| 4554 | return nullptr; |
| 4555 | |
| 4556 | auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4557 | if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt) |
| 4558 | return nullptr; |
| 4559 | auto *LoadedLHSPtr = LoadedLHS->getOperand(0); |
| 4560 | |
| 4561 | Constant *LoadedRHS = LoadedCE->getOperand(1); |
| 4562 | GlobalValue *LoadedRHSSym; |
| 4563 | APInt LoadedRHSOffset; |
| 4564 | if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset, |
| 4565 | DL) || |
| 4566 | PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset) |
| 4567 | return nullptr; |
| 4568 | |
| 4569 | return ConstantExpr::getBitCast(LoadedLHSPtr, Int8PtrTy); |
| 4570 | } |
| 4571 | |
David Majnemer | 17a95aa | 2016-07-14 06:58:37 +0000 | [diff] [blame] | 4572 | static bool maskIsAllZeroOrUndef(Value *Mask) { |
| 4573 | auto *ConstMask = dyn_cast<Constant>(Mask); |
| 4574 | if (!ConstMask) |
| 4575 | return false; |
| 4576 | if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask)) |
| 4577 | return true; |
| 4578 | for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E; |
| 4579 | ++I) { |
| 4580 | if (auto *MaskElt = ConstMask->getAggregateElement(I)) |
| 4581 | if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt)) |
| 4582 | continue; |
| 4583 | return false; |
| 4584 | } |
| 4585 | return true; |
| 4586 | } |
| 4587 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4588 | template <typename IterTy> |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4589 | static Value *SimplifyIntrinsic(Function *F, IterTy ArgBegin, IterTy ArgEnd, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4590 | const SimplifyQuery &Q, unsigned MaxRecurse) { |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4591 | Intrinsic::ID IID = F->getIntrinsicID(); |
| 4592 | unsigned NumOperands = std::distance(ArgBegin, ArgEnd); |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4593 | |
| 4594 | // Unary Ops |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4595 | if (NumOperands == 1) { |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4596 | // Perform idempotent optimizations |
| 4597 | if (IsIdempotent(IID)) { |
| 4598 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin)) { |
| 4599 | if (II->getIntrinsicID() == IID) |
| 4600 | return II; |
| 4601 | } |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Dmitry Venikov | 3d8cd34 | 2018-01-03 14:37:42 +0000 | [diff] [blame] | 4604 | Value *IIOperand = *ArgBegin; |
| 4605 | Value *X; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4606 | switch (IID) { |
| 4607 | case Intrinsic::fabs: { |
Dmitry Venikov | 3d8cd34 | 2018-01-03 14:37:42 +0000 | [diff] [blame] | 4608 | if (SignBitMustBeZero(IIOperand, Q.TLI)) |
| 4609 | return IIOperand; |
Marcello Maggioni | 0616b5f | 2017-01-14 07:28:47 +0000 | [diff] [blame] | 4610 | return nullptr; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4611 | } |
Philip Reames | 5000ba6 | 2017-12-27 01:14:30 +0000 | [diff] [blame] | 4612 | case Intrinsic::bswap: { |
Philip Reames | 5000ba6 | 2017-12-27 01:14:30 +0000 | [diff] [blame] | 4613 | // bswap(bswap(x)) -> x |
| 4614 | if (match(IIOperand, m_BSwap(m_Value(X)))) |
| 4615 | return X; |
| 4616 | return nullptr; |
| 4617 | } |
| 4618 | case Intrinsic::bitreverse: { |
Philip Reames | 5000ba6 | 2017-12-27 01:14:30 +0000 | [diff] [blame] | 4619 | // bitreverse(bitreverse(x)) -> x |
| 4620 | if (match(IIOperand, m_BitReverse(m_Value(X)))) |
| 4621 | return X; |
| 4622 | return nullptr; |
| 4623 | } |
Dmitry Venikov | 3d8cd34 | 2018-01-03 14:37:42 +0000 | [diff] [blame] | 4624 | case Intrinsic::exp: { |
| 4625 | // exp(log(x)) -> x |
Sanjay Patel | 246d7692 | 2018-02-12 23:51:23 +0000 | [diff] [blame] | 4626 | if (Q.CxtI->hasAllowReassoc() && |
Dmitry Venikov | 3d8cd34 | 2018-01-03 14:37:42 +0000 | [diff] [blame] | 4627 | match(IIOperand, m_Intrinsic<Intrinsic::log>(m_Value(X)))) |
| 4628 | return X; |
| 4629 | return nullptr; |
| 4630 | } |
| 4631 | case Intrinsic::exp2: { |
| 4632 | // exp2(log2(x)) -> x |
Sanjay Patel | 246d7692 | 2018-02-12 23:51:23 +0000 | [diff] [blame] | 4633 | if (Q.CxtI->hasAllowReassoc() && |
Dmitry Venikov | 3d8cd34 | 2018-01-03 14:37:42 +0000 | [diff] [blame] | 4634 | match(IIOperand, m_Intrinsic<Intrinsic::log2>(m_Value(X)))) |
| 4635 | return X; |
| 4636 | return nullptr; |
| 4637 | } |
| 4638 | case Intrinsic::log: { |
| 4639 | // log(exp(x)) -> x |
Sanjay Patel | 246d7692 | 2018-02-12 23:51:23 +0000 | [diff] [blame] | 4640 | if (Q.CxtI->hasAllowReassoc() && |
Dmitry Venikov | 3d8cd34 | 2018-01-03 14:37:42 +0000 | [diff] [blame] | 4641 | match(IIOperand, m_Intrinsic<Intrinsic::exp>(m_Value(X)))) |
| 4642 | return X; |
| 4643 | return nullptr; |
| 4644 | } |
| 4645 | case Intrinsic::log2: { |
| 4646 | // log2(exp2(x)) -> x |
Sanjay Patel | 246d7692 | 2018-02-12 23:51:23 +0000 | [diff] [blame] | 4647 | if (Q.CxtI->hasAllowReassoc() && |
Dmitry Venikov | 3d8cd34 | 2018-01-03 14:37:42 +0000 | [diff] [blame] | 4648 | match(IIOperand, m_Intrinsic<Intrinsic::exp2>(m_Value(X)))) { |
| 4649 | return X; |
| 4650 | } |
| 4651 | return nullptr; |
| 4652 | } |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4653 | default: |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4654 | return nullptr; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4655 | } |
| 4656 | } |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4657 | |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4658 | // Binary Ops |
| 4659 | if (NumOperands == 2) { |
| 4660 | Value *LHS = *ArgBegin; |
| 4661 | Value *RHS = *(ArgBegin + 1); |
| 4662 | Type *ReturnType = F->getReturnType(); |
| 4663 | |
| 4664 | switch (IID) { |
| 4665 | case Intrinsic::usub_with_overflow: |
| 4666 | case Intrinsic::ssub_with_overflow: { |
| 4667 | // X - X -> { 0, false } |
| 4668 | if (LHS == RHS) |
| 4669 | return Constant::getNullValue(ReturnType); |
| 4670 | |
| 4671 | // X - undef -> undef |
| 4672 | // undef - X -> undef |
| 4673 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) |
| 4674 | return UndefValue::get(ReturnType); |
| 4675 | |
| 4676 | return nullptr; |
| 4677 | } |
| 4678 | case Intrinsic::uadd_with_overflow: |
| 4679 | case Intrinsic::sadd_with_overflow: { |
| 4680 | // X + undef -> undef |
Craig Topper | 77e07cc | 2017-05-24 17:05:28 +0000 | [diff] [blame] | 4681 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4682 | return UndefValue::get(ReturnType); |
| 4683 | |
| 4684 | return nullptr; |
| 4685 | } |
| 4686 | case Intrinsic::umul_with_overflow: |
| 4687 | case Intrinsic::smul_with_overflow: { |
Craig Topper | 77e07cc | 2017-05-24 17:05:28 +0000 | [diff] [blame] | 4688 | // 0 * X -> { 0, false } |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4689 | // X * 0 -> { 0, false } |
Craig Topper | 77e07cc | 2017-05-24 17:05:28 +0000 | [diff] [blame] | 4690 | if (match(LHS, m_Zero()) || match(RHS, m_Zero())) |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4691 | return Constant::getNullValue(ReturnType); |
| 4692 | |
Craig Topper | 77e07cc | 2017-05-24 17:05:28 +0000 | [diff] [blame] | 4693 | // undef * X -> { 0, false } |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4694 | // X * undef -> { 0, false } |
Craig Topper | 77e07cc | 2017-05-24 17:05:28 +0000 | [diff] [blame] | 4695 | if (match(LHS, m_Undef()) || match(RHS, m_Undef())) |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4696 | return Constant::getNullValue(ReturnType); |
| 4697 | |
| 4698 | return nullptr; |
| 4699 | } |
| 4700 | case Intrinsic::load_relative: { |
| 4701 | Constant *C0 = dyn_cast<Constant>(LHS); |
| 4702 | Constant *C1 = dyn_cast<Constant>(RHS); |
| 4703 | if (C0 && C1) |
| 4704 | return SimplifyRelativeLoad(C0, C1, Q.DL); |
| 4705 | return nullptr; |
| 4706 | } |
Philip Reames | 5000ba6 | 2017-12-27 01:14:30 +0000 | [diff] [blame] | 4707 | case Intrinsic::powi: |
| 4708 | if (ConstantInt *Power = dyn_cast<ConstantInt>(RHS)) { |
| 4709 | // powi(x, 0) -> 1.0 |
| 4710 | if (Power->isZero()) |
| 4711 | return ConstantFP::get(LHS->getType(), 1.0); |
| 4712 | // powi(x, 1) -> x |
| 4713 | if (Power->isOne()) |
| 4714 | return LHS; |
| 4715 | } |
| 4716 | return nullptr; |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4717 | default: |
| 4718 | return nullptr; |
| 4719 | } |
| 4720 | } |
| 4721 | |
| 4722 | // Simplify calls to llvm.masked.load.* |
| 4723 | switch (IID) { |
| 4724 | case Intrinsic::masked_load: { |
| 4725 | Value *MaskArg = ArgBegin[2]; |
| 4726 | Value *PassthruArg = ArgBegin[3]; |
| 4727 | // If the mask is all zeros or undef, the "passthru" argument is the result. |
| 4728 | if (maskIsAllZeroOrUndef(MaskArg)) |
| 4729 | return PassthruArg; |
| 4730 | return nullptr; |
| 4731 | } |
| 4732 | default: |
| 4733 | return nullptr; |
| 4734 | } |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4737 | template <typename IterTy> |
Andrew Kaylor | 647025f | 2017-06-09 23:18:11 +0000 | [diff] [blame] | 4738 | static Value *SimplifyCall(ImmutableCallSite CS, Value *V, IterTy ArgBegin, |
| 4739 | IterTy ArgEnd, const SimplifyQuery &Q, |
| 4740 | unsigned MaxRecurse) { |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4741 | Type *Ty = V->getType(); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4742 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) |
| 4743 | Ty = PTy->getElementType(); |
| 4744 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 4745 | |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4746 | // call undef -> undef |
David Majnemer | bb53d23 | 2016-06-25 07:37:30 +0000 | [diff] [blame] | 4747 | // call null -> undef |
| 4748 | if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V)) |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4749 | return UndefValue::get(FTy->getReturnType()); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4750 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4751 | Function *F = dyn_cast<Function>(V); |
| 4752 | if (!F) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4753 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4754 | |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4755 | if (F->isIntrinsic()) |
| 4756 | if (Value *Ret = SimplifyIntrinsic(F, ArgBegin, ArgEnd, Q, MaxRecurse)) |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4757 | return Ret; |
| 4758 | |
Andrew Kaylor | 647025f | 2017-06-09 23:18:11 +0000 | [diff] [blame] | 4759 | if (!canConstantFoldCallTo(CS, F)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4760 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4761 | |
| 4762 | SmallVector<Constant *, 4> ConstantArgs; |
| 4763 | ConstantArgs.reserve(ArgEnd - ArgBegin); |
| 4764 | for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) { |
| 4765 | Constant *C = dyn_cast<Constant>(*I); |
| 4766 | if (!C) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4767 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4768 | ConstantArgs.push_back(C); |
| 4769 | } |
| 4770 | |
Andrew Kaylor | 647025f | 2017-06-09 23:18:11 +0000 | [diff] [blame] | 4771 | return ConstantFoldCall(CS, F, ConstantArgs, Q.TLI); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Andrew Kaylor | 647025f | 2017-06-09 23:18:11 +0000 | [diff] [blame] | 4774 | Value *llvm::SimplifyCall(ImmutableCallSite CS, Value *V, |
| 4775 | User::op_iterator ArgBegin, User::op_iterator ArgEnd, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4776 | const SimplifyQuery &Q) { |
Andrew Kaylor | 647025f | 2017-06-09 23:18:11 +0000 | [diff] [blame] | 4777 | return ::SimplifyCall(CS, V, ArgBegin, ArgEnd, Q, RecursionLimit); |
| 4778 | } |
| 4779 | |
| 4780 | Value *llvm::SimplifyCall(ImmutableCallSite CS, Value *V, |
| 4781 | ArrayRef<Value *> Args, const SimplifyQuery &Q) { |
| 4782 | return ::SimplifyCall(CS, V, Args.begin(), Args.end(), Q, RecursionLimit); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
Philip Reames | 7a6db4f | 2017-12-27 00:16:12 +0000 | [diff] [blame] | 4785 | Value *llvm::SimplifyCall(ImmutableCallSite ICS, const SimplifyQuery &Q) { |
| 4786 | CallSite CS(const_cast<Instruction*>(ICS.getInstruction())); |
| 4787 | return ::SimplifyCall(CS, CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), |
| 4788 | Q, RecursionLimit); |
| 4789 | } |
| 4790 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4791 | /// See if we can compute a simplified version of this instruction. |
| 4792 | /// If not, this returns null. |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4793 | |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 4794 | Value *llvm::SimplifyInstruction(Instruction *I, const SimplifyQuery &SQ, |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4795 | OptimizationRemarkEmitter *ORE) { |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 4796 | const SimplifyQuery Q = SQ.CxtI ? SQ : SQ.getWithInstruction(I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4797 | Value *Result; |
| 4798 | |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4799 | switch (I->getOpcode()) { |
| 4800 | default: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4801 | Result = ConstantFoldInstruction(I, Q.DL, Q.TLI); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4802 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4803 | case Instruction::FAdd: |
| 4804 | Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4805 | I->getFastMathFlags(), Q); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4806 | break; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 4807 | case Instruction::Add: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4808 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 4809 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4810 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), Q); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4811 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4812 | case Instruction::FSub: |
| 4813 | Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4814 | I->getFastMathFlags(), Q); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4815 | break; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4816 | case Instruction::Sub: |
| 4817 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 4818 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4819 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), Q); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4820 | break; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4821 | case Instruction::FMul: |
| 4822 | Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4823 | I->getFastMathFlags(), Q); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4824 | break; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4825 | case Instruction::Mul: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4826 | Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), Q); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4827 | break; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4828 | case Instruction::SDiv: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4829 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), Q); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4830 | break; |
| 4831 | case Instruction::UDiv: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4832 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), Q); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4833 | break; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4834 | case Instruction::FDiv: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4835 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4836 | I->getFastMathFlags(), Q); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4837 | break; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4838 | case Instruction::SRem: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4839 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), Q); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4840 | break; |
| 4841 | case Instruction::URem: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4842 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), Q); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4843 | break; |
| 4844 | case Instruction::FRem: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4845 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4846 | I->getFastMathFlags(), Q); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4847 | break; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4848 | case Instruction::Shl: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4849 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 4850 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4851 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), Q); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4852 | break; |
| 4853 | case Instruction::LShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4854 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4855 | cast<BinaryOperator>(I)->isExact(), Q); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4856 | break; |
| 4857 | case Instruction::AShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4858 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4859 | cast<BinaryOperator>(I)->isExact(), Q); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4860 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4861 | case Instruction::And: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4862 | Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), Q); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4863 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4864 | case Instruction::Or: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4865 | Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), Q); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4866 | break; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4867 | case Instruction::Xor: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4868 | Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), Q); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4869 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4870 | case Instruction::ICmp: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4871 | Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), |
| 4872 | I->getOperand(0), I->getOperand(1), Q); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4873 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4874 | case Instruction::FCmp: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4875 | Result = |
| 4876 | SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), I->getOperand(0), |
| 4877 | I->getOperand(1), I->getFastMathFlags(), Q); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4878 | break; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 4879 | case Instruction::Select: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4880 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4881 | I->getOperand(2), Q); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4882 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4883 | case Instruction::GetElementPtr: { |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4884 | SmallVector<Value *, 8> Ops(I->op_begin(), I->op_end()); |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 4885 | Result = SimplifyGEPInst(cast<GetElementPtrInst>(I)->getSourceElementType(), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4886 | Ops, Q); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4887 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4888 | } |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4889 | case Instruction::InsertValue: { |
| 4890 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 4891 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 4892 | IV->getInsertedValueOperand(), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4893 | IV->getIndices(), Q); |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4894 | break; |
| 4895 | } |
Igor Laevsky | e0edb66 | 2017-12-13 11:21:18 +0000 | [diff] [blame] | 4896 | case Instruction::InsertElement: { |
| 4897 | auto *IE = cast<InsertElementInst>(I); |
| 4898 | Result = SimplifyInsertElementInst(IE->getOperand(0), IE->getOperand(1), |
| 4899 | IE->getOperand(2), Q); |
| 4900 | break; |
| 4901 | } |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 4902 | case Instruction::ExtractValue: { |
| 4903 | auto *EVI = cast<ExtractValueInst>(I); |
| 4904 | Result = SimplifyExtractValueInst(EVI->getAggregateOperand(), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4905 | EVI->getIndices(), Q); |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 4906 | break; |
| 4907 | } |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4908 | case Instruction::ExtractElement: { |
| 4909 | auto *EEI = cast<ExtractElementInst>(I); |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4910 | Result = SimplifyExtractElementInst(EEI->getVectorOperand(), |
| 4911 | EEI->getIndexOperand(), Q); |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4912 | break; |
| 4913 | } |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4914 | case Instruction::ShuffleVector: { |
| 4915 | auto *SVI = cast<ShuffleVectorInst>(I); |
| 4916 | Result = SimplifyShuffleVectorInst(SVI->getOperand(0), SVI->getOperand(1), |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4917 | SVI->getMask(), SVI->getType(), Q); |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4918 | break; |
| 4919 | } |
Duncan Sands | 4581ddc | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 4920 | case Instruction::PHI: |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4921 | Result = SimplifyPHINode(cast<PHINode>(I), Q); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4922 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4923 | case Instruction::Call: { |
| 4924 | CallSite CS(cast<CallInst>(I)); |
Philip Reames | 7a6db4f | 2017-12-27 00:16:12 +0000 | [diff] [blame] | 4925 | Result = SimplifyCall(CS, Q); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4926 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4927 | } |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4928 | #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc: |
| 4929 | #include "llvm/IR/Instruction.def" |
| 4930 | #undef HANDLE_CAST_INST |
Daniel Berlin | 5e3fcb1 | 2017-04-26 04:09:56 +0000 | [diff] [blame] | 4931 | Result = |
| 4932 | SimplifyCastInst(I->getOpcode(), I->getOperand(0), I->getType(), Q); |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4933 | break; |
Craig Topper | 81c03a7 | 2017-04-12 22:54:24 +0000 | [diff] [blame] | 4934 | case Instruction::Alloca: |
| 4935 | // No simplifications for Alloca and it can't be constant folded. |
| 4936 | Result = nullptr; |
| 4937 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4938 | } |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4939 | |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4940 | // In general, it is possible for computeKnownBits to determine all bits in a |
| 4941 | // value even when the operands are not all constants. |
Sanjay Patel | 8ca30ab | 2016-11-27 21:07:28 +0000 | [diff] [blame] | 4942 | if (!Result && I->getType()->isIntOrIntVectorTy()) { |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 4943 | KnownBits Known = computeKnownBits(I, Q.DL, /*Depth*/ 0, Q.AC, I, Q.DT, ORE); |
Craig Topper | 8189a87 | 2017-05-03 23:12:29 +0000 | [diff] [blame] | 4944 | if (Known.isConstant()) |
| 4945 | Result = ConstantInt::get(I->getType(), Known.getConstant()); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4946 | } |
| 4947 | |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4948 | /// If called on unreachable code, the above logic may report that the |
| 4949 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 4950 | /// detecting that case here, returning a safe value instead. |
| 4951 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4954 | /// Implementation of recursive simplification through an instruction's |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4955 | /// uses. |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4956 | /// |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4957 | /// This is the common implementation of the recursive simplification routines. |
| 4958 | /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to |
| 4959 | /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of |
| 4960 | /// instructions to process and attempt to simplify it using |
| 4961 | /// InstructionSimplify. |
| 4962 | /// |
| 4963 | /// This routine returns 'true' only when *it* simplifies something. The passed |
| 4964 | /// in simplified value does not count toward this. |
| 4965 | static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4966 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4967 | const DominatorTree *DT, |
| 4968 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4969 | bool Simplified = false; |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4970 | SmallSetVector<Instruction *, 8> Worklist; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4971 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4972 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4973 | // If we have an explicit value to collapse to, do that round of the |
| 4974 | // simplification loop by hand initially. |
| 4975 | if (SimpleV) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4976 | for (User *U : I->users()) |
| 4977 | if (U != I) |
| 4978 | Worklist.insert(cast<Instruction>(U)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4979 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4980 | // Replace the instruction with its simplified value. |
| 4981 | I->replaceAllUsesWith(SimpleV); |
Chris Lattner | 19eff2a | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 4982 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4983 | // Gracefully handle edge cases where the instruction is not wired into any |
| 4984 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 4985 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 4986 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4987 | I->eraseFromParent(); |
| 4988 | } else { |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4989 | Worklist.insert(I); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4990 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4991 | |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4992 | // Note that we must test the size on each iteration, the worklist can grow. |
| 4993 | for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { |
| 4994 | I = Worklist[Idx]; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4995 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4996 | // See if this instruction simplifies. |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 4997 | SimpleV = SimplifyInstruction(I, {DL, TLI, DT, AC}); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4998 | if (!SimpleV) |
| 4999 | continue; |
| 5000 | |
| 5001 | Simplified = true; |
| 5002 | |
| 5003 | // Stash away all the uses of the old instruction so we can check them for |
| 5004 | // recursive simplifications after a RAUW. This is cheaper than checking all |
| 5005 | // uses of To on the recursive step in most cases. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 5006 | for (User *U : I->users()) |
| 5007 | Worklist.insert(cast<Instruction>(U)); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 5008 | |
| 5009 | // Replace the instruction with its simplified value. |
| 5010 | I->replaceAllUsesWith(SimpleV); |
| 5011 | |
| 5012 | // Gracefully handle edge cases where the instruction is not wired into any |
| 5013 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 5014 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 5015 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 5016 | I->eraseFromParent(); |
| 5017 | } |
| 5018 | return Simplified; |
| 5019 | } |
| 5020 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 5021 | bool llvm::recursivelySimplifyInstruction(Instruction *I, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 5022 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 5023 | const DominatorTree *DT, |
| 5024 | AssumptionCache *AC) { |
| 5025 | return replaceAndRecursivelySimplifyImpl(I, nullptr, TLI, DT, AC); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 5029 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 5030 | const DominatorTree *DT, |
| 5031 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 5032 | assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!"); |
| 5033 | assert(SimpleV && "Must provide a simplified value."); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 5034 | return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 5035 | } |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 5036 | |
| 5037 | namespace llvm { |
| 5038 | const SimplifyQuery getBestSimplifyQuery(Pass &P, Function &F) { |
| 5039 | auto *DTWP = P.getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 5040 | auto *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
| 5041 | auto *TLIWP = P.getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>(); |
| 5042 | auto *TLI = TLIWP ? &TLIWP->getTLI() : nullptr; |
| 5043 | auto *ACWP = P.getAnalysisIfAvailable<AssumptionCacheTracker>(); |
| 5044 | auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr; |
| 5045 | return {F.getParent()->getDataLayout(), TLI, DT, AC}; |
| 5046 | } |
| 5047 | |
| 5048 | const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &AR, |
| 5049 | const DataLayout &DL) { |
| 5050 | return {DL, &AR.TLI, &AR.DT, &AR.AC}; |
| 5051 | } |
| 5052 | |
| 5053 | template <class T, class... TArgs> |
| 5054 | const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &AM, |
| 5055 | Function &F) { |
| 5056 | auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(F); |
| 5057 | auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(F); |
| 5058 | auto *AC = AM.template getCachedResult<AssumptionAnalysis>(F); |
| 5059 | return {F.getParent()->getDataLayout(), TLI, DT, AC}; |
| 5060 | } |
| 5061 | template const SimplifyQuery getBestSimplifyQuery(AnalysisManager<Function> &, |
| 5062 | Function &); |
| 5063 | } |