blob: eb49f904ea40ecad7e900161d4d69a94e1d473b0 [file] [log] [blame]
Chris Lattner965c7692008-06-02 01:18:21 +00001//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner965c7692008-06-02 01:18:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000015#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/None.h"
James Molloy493e57d2015-10-26 14:10:46 +000019#include "llvm/ADT/Optional.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000020#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000022#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/iterator_range.h"
26#include "llvm/Analysis/AliasAnalysis.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000027#include "llvm/Analysis/AssumptionCache.h"
Max Kazantsev3c284bd2018-08-30 03:39:16 +000028#include "llvm/Analysis/GuardUtils.h"
Dan Gohman949ab782010-12-15 20:10:26 +000029#include "llvm/Analysis/InstructionSimplify.h"
Artur Pilipenko31bcca42016-02-24 12:49:04 +000030#include "llvm/Analysis/Loads.h"
Adam Nemete2b885c2015-04-23 20:09:20 +000031#include "llvm/Analysis/LoopInfo.h"
Adam Nemet0965da22017-10-09 23:19:02 +000032#include "llvm/Analysis/OptimizationRemarkEmitter.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000033#include "llvm/Analysis/TargetLibraryInfo.h"
34#include "llvm/IR/Argument.h"
35#include "llvm/IR/Attributes.h"
36#include "llvm/IR/BasicBlock.h"
Nick Lewyckyec373542014-05-20 05:13:21 +000037#include "llvm/IR/CallSite.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000038#include "llvm/IR/Constant.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000039#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Constants.h"
41#include "llvm/IR/DataLayout.h"
Matthias Braun50ec0b52017-05-19 22:37:09 +000042#include "llvm/IR/DerivedTypes.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000043#include "llvm/IR/DiagnosticInfo.h"
Hal Finkel60db0582014-09-07 18:57:58 +000044#include "llvm/IR/Dominators.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000045#include "llvm/IR/Function.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000046#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000047#include "llvm/IR/GlobalAlias.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000048#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000049#include "llvm/IR/GlobalVariable.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000050#include "llvm/IR/InstrTypes.h"
51#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000052#include "llvm/IR/Instructions.h"
53#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000054#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000055#include "llvm/IR/LLVMContext.h"
56#include "llvm/IR/Metadata.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000057#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000058#include "llvm/IR/Operator.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000059#include "llvm/IR/PatternMatch.h"
Eugene Zelenko75075ef2017-09-01 21:37:29 +000060#include "llvm/IR/Type.h"
61#include "llvm/IR/User.h"
62#include "llvm/IR/Value.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/CommandLine.h"
65#include "llvm/Support/Compiler.h"
66#include "llvm/Support/ErrorHandling.h"
Craig Topperb45eabc2017-04-26 16:39:58 +000067#include "llvm/Support/KnownBits.h"
Chris Lattner965c7692008-06-02 01:18:21 +000068#include "llvm/Support/MathExtras.h"
Matthias Braun37e5d792016-01-28 06:29:33 +000069#include <algorithm>
70#include <array>
Eugene Zelenko75075ef2017-09-01 21:37:29 +000071#include <cassert>
72#include <cstdint>
73#include <iterator>
Fangrui Songf78650a2018-07-30 19:41:25 +000074#include <utility>
Eugene Zelenko75075ef2017-09-01 21:37:29 +000075
Chris Lattner965c7692008-06-02 01:18:21 +000076using namespace llvm;
Duncan Sandsd3951082011-01-25 09:38:29 +000077using namespace llvm::PatternMatch;
78
79const unsigned MaxDepth = 6;
80
Philip Reames1c292272015-03-10 22:43:20 +000081// Controls the number of uses of the value searched for possible
82// dominating comparisons.
83static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
Igor Laevskycea9ede2015-09-29 14:57:52 +000084 cl::Hidden, cl::init(20));
Philip Reames1c292272015-03-10 22:43:20 +000085
Craig Topper6b3940a2017-05-03 22:25:19 +000086/// Returns the bitwidth of the given scalar or pointer type. For vector types,
87/// returns the element type's bitwidth.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000088static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
Duncan Sandsd3951082011-01-25 09:38:29 +000089 if (unsigned BitWidth = Ty->getScalarSizeInBits())
90 return BitWidth;
Matt Arsenaultf55e5e72013-08-10 17:34:08 +000091
Elena Demikhovsky945b7e52018-02-14 06:58:08 +000092 return DL.getIndexTypeSizeInBits(Ty);
Duncan Sandsd3951082011-01-25 09:38:29 +000093}
Chris Lattner965c7692008-06-02 01:18:21 +000094
Benjamin Kramercfd8d902014-09-12 08:56:53 +000095namespace {
Eugene Zelenko75075ef2017-09-01 21:37:29 +000096
Hal Finkel60db0582014-09-07 18:57:58 +000097// Simplifying using an assume can only be done in a particular control-flow
98// context (the context instruction provides that context). If an assume and
99// the context instruction are not in the same block then the DT helps in
100// figuring out if we can use it.
101struct Query {
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000102 const DataLayout &DL;
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000103 AssumptionCache *AC;
Hal Finkel60db0582014-09-07 18:57:58 +0000104 const Instruction *CxtI;
105 const DominatorTree *DT;
Eugene Zelenko75075ef2017-09-01 21:37:29 +0000106
Sanjay Patel54656ca2017-02-06 18:26:06 +0000107 // Unlike the other analyses, this may be a nullptr because not all clients
108 // provide it currently.
109 OptimizationRemarkEmitter *ORE;
Hal Finkel60db0582014-09-07 18:57:58 +0000110
Matthias Braun37e5d792016-01-28 06:29:33 +0000111 /// Set of assumptions that should be excluded from further queries.
112 /// This is because of the potential for mutual recursion to cause
113 /// computeKnownBits to repeatedly visit the same assume intrinsic. The
114 /// classic case of this is assume(x = y), which will attempt to determine
115 /// bits in x from bits in y, which will attempt to determine bits in y from
116 /// bits in x, etc. Regarding the mutual recursion, computeKnownBits can call
Craig Topper6e11a052017-05-08 16:22:48 +0000117 /// isKnownNonZero, which calls computeKnownBits and isKnownToBeAPowerOfTwo
118 /// (all of which can call computeKnownBits), and so on.
Li Huang755f75f2016-10-15 19:00:04 +0000119 std::array<const Value *, MaxDepth> Excluded;
Eugene Zelenko75075ef2017-09-01 21:37:29 +0000120
Florian Hahn19f9e322018-08-17 14:39:04 +0000121 /// If true, it is safe to use metadata during simplification.
122 InstrInfoQuery IIQ;
123
Eugene Zelenko75075ef2017-09-01 21:37:29 +0000124 unsigned NumExcluded = 0;
Matthias Braun37e5d792016-01-28 06:29:33 +0000125
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000126 Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI,
Florian Hahn19f9e322018-08-17 14:39:04 +0000127 const DominatorTree *DT, bool UseInstrInfo,
128 OptimizationRemarkEmitter *ORE = nullptr)
129 : DL(DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {}
Hal Finkel60db0582014-09-07 18:57:58 +0000130
131 Query(const Query &Q, const Value *NewExcl)
Florian Hahn19f9e322018-08-17 14:39:04 +0000132 : DL(Q.DL), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT), ORE(Q.ORE), IIQ(Q.IIQ),
Sanjay Patel54656ca2017-02-06 18:26:06 +0000133 NumExcluded(Q.NumExcluded) {
Matthias Braun37e5d792016-01-28 06:29:33 +0000134 Excluded = Q.Excluded;
135 Excluded[NumExcluded++] = NewExcl;
136 assert(NumExcluded <= Excluded.size());
137 }
138
139 bool isExcluded(const Value *Value) const {
140 if (NumExcluded == 0)
141 return false;
142 auto End = Excluded.begin() + NumExcluded;
143 return std::find(Excluded.begin(), End, Value) != End;
Hal Finkel60db0582014-09-07 18:57:58 +0000144 }
145};
Eugene Zelenko75075ef2017-09-01 21:37:29 +0000146
Benjamin Kramercfd8d902014-09-12 08:56:53 +0000147} // end anonymous namespace
Hal Finkel60db0582014-09-07 18:57:58 +0000148
Sanjay Patel547e9752014-11-04 16:09:50 +0000149// Given the provided Value and, potentially, a context instruction, return
Hal Finkel60db0582014-09-07 18:57:58 +0000150// the preferred context instruction (if any).
151static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
152 // If we've been provided with a context instruction, then use that (provided
153 // it has been inserted).
154 if (CxtI && CxtI->getParent())
155 return CxtI;
156
157 // If the value is really an already-inserted instruction, then use that.
158 CxtI = dyn_cast<Instruction>(V);
159 if (CxtI && CxtI->getParent())
160 return CxtI;
161
162 return nullptr;
163}
164
Craig Topperb45eabc2017-04-26 16:39:58 +0000165static void computeKnownBits(const Value *V, KnownBits &Known,
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000166 unsigned Depth, const Query &Q);
Hal Finkel60db0582014-09-07 18:57:58 +0000167
Craig Topperb45eabc2017-04-26 16:39:58 +0000168void llvm::computeKnownBits(const Value *V, KnownBits &Known,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000169 const DataLayout &DL, unsigned Depth,
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000170 AssumptionCache *AC, const Instruction *CxtI,
Sanjay Patel54656ca2017-02-06 18:26:06 +0000171 const DominatorTree *DT,
Florian Hahn19f9e322018-08-17 14:39:04 +0000172 OptimizationRemarkEmitter *ORE, bool UseInstrInfo) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000173 ::computeKnownBits(V, Known, Depth,
Florian Hahn19f9e322018-08-17 14:39:04 +0000174 Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
Hal Finkel60db0582014-09-07 18:57:58 +0000175}
176
Craig Topper6e11a052017-05-08 16:22:48 +0000177static KnownBits computeKnownBits(const Value *V, unsigned Depth,
178 const Query &Q);
179
180KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL,
181 unsigned Depth, AssumptionCache *AC,
182 const Instruction *CxtI,
Craig Toppera2025ea2017-05-24 16:53:03 +0000183 const DominatorTree *DT,
Florian Hahn19f9e322018-08-17 14:39:04 +0000184 OptimizationRemarkEmitter *ORE,
185 bool UseInstrInfo) {
186 return ::computeKnownBits(
187 V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
Craig Topper6e11a052017-05-08 16:22:48 +0000188}
189
Pete Cooper35b00d52016-08-13 01:05:32 +0000190bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
Florian Hahn19f9e322018-08-17 14:39:04 +0000191 const DataLayout &DL, AssumptionCache *AC,
192 const Instruction *CxtI, const DominatorTree *DT,
193 bool UseInstrInfo) {
Jingyue Wuca321902015-05-14 23:53:19 +0000194 assert(LHS->getType() == RHS->getType() &&
195 "LHS and RHS should have the same type");
196 assert(LHS->getType()->isIntOrIntVectorTy() &&
197 "LHS and RHS should be integers");
Roman Lebedev620b3da2018-04-15 18:59:33 +0000198 // Look for an inverted mask: (X & ~M) op (Y & M).
199 Value *M;
200 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
201 match(RHS, m_c_And(m_Specific(M), m_Value())))
202 return true;
203 if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
204 match(LHS, m_c_And(m_Specific(M), m_Value())))
205 return true;
Jingyue Wuca321902015-05-14 23:53:19 +0000206 IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType());
Craig Topperb45eabc2017-04-26 16:39:58 +0000207 KnownBits LHSKnown(IT->getBitWidth());
208 KnownBits RHSKnown(IT->getBitWidth());
Florian Hahn19f9e322018-08-17 14:39:04 +0000209 computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
210 computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
Craig Topperb45eabc2017-04-26 16:39:58 +0000211 return (LHSKnown.Zero | RHSKnown.Zero).isAllOnesValue();
Jingyue Wuca321902015-05-14 23:53:19 +0000212}
213
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000214bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI) {
215 for (const User *U : CxtI->users()) {
216 if (const ICmpInst *IC = dyn_cast<ICmpInst>(U))
217 if (IC->isEquality())
218 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
219 if (C->isNullValue())
220 continue;
221 return false;
222 }
223 return true;
224}
225
Pete Cooper35b00d52016-08-13 01:05:32 +0000226static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000227 const Query &Q);
Hal Finkel60db0582014-09-07 18:57:58 +0000228
Pete Cooper35b00d52016-08-13 01:05:32 +0000229bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
Florian Hahn19f9e322018-08-17 14:39:04 +0000230 bool OrZero, unsigned Depth,
231 AssumptionCache *AC, const Instruction *CxtI,
232 const DominatorTree *DT, bool UseInstrInfo) {
233 return ::isKnownToBeAPowerOfTwo(
234 V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000235}
236
Pete Cooper35b00d52016-08-13 01:05:32 +0000237static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000238
Pete Cooper35b00d52016-08-13 01:05:32 +0000239bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth,
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000240 AssumptionCache *AC, const Instruction *CxtI,
Florian Hahn19f9e322018-08-17 14:39:04 +0000241 const DominatorTree *DT, bool UseInstrInfo) {
242 return ::isKnownNonZero(V, Depth,
243 Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000244}
245
Pete Cooper35b00d52016-08-13 01:05:32 +0000246bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL,
Florian Hahn19f9e322018-08-17 14:39:04 +0000247 unsigned Depth, AssumptionCache *AC,
248 const Instruction *CxtI, const DominatorTree *DT,
249 bool UseInstrInfo) {
250 KnownBits Known =
251 computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
Craig Topper6e11a052017-05-08 16:22:48 +0000252 return Known.isNonNegative();
Jingyue Wu10fcea52015-08-20 18:27:04 +0000253}
254
Pete Cooper35b00d52016-08-13 01:05:32 +0000255bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth,
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000256 AssumptionCache *AC, const Instruction *CxtI,
Florian Hahn19f9e322018-08-17 14:39:04 +0000257 const DominatorTree *DT, bool UseInstrInfo) {
Philip Reames8f12eba2016-03-09 21:31:47 +0000258 if (auto *CI = dyn_cast<ConstantInt>(V))
259 return CI->getValue().isStrictlyPositive();
Sanjoy Das6082c1a2016-05-07 02:08:15 +0000260
Philip Reames8f12eba2016-03-09 21:31:47 +0000261 // TODO: We'd doing two recursive queries here. We should factor this such
262 // that only a single query is needed.
Florian Hahn19f9e322018-08-17 14:39:04 +0000263 return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) &&
264 isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo);
Philip Reames8f12eba2016-03-09 21:31:47 +0000265}
266
Pete Cooper35b00d52016-08-13 01:05:32 +0000267bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth,
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000268 AssumptionCache *AC, const Instruction *CxtI,
Florian Hahn19f9e322018-08-17 14:39:04 +0000269 const DominatorTree *DT, bool UseInstrInfo) {
270 KnownBits Known =
271 computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
Craig Topper6e11a052017-05-08 16:22:48 +0000272 return Known.isNegative();
Nick Lewycky762f8a82016-04-21 00:53:14 +0000273}
274
Pete Cooper35b00d52016-08-13 01:05:32 +0000275static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q);
James Molloy1d88d6f2015-10-22 13:18:42 +0000276
Pete Cooper35b00d52016-08-13 01:05:32 +0000277bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
Florian Hahn19f9e322018-08-17 14:39:04 +0000278 const DataLayout &DL, AssumptionCache *AC,
279 const Instruction *CxtI, const DominatorTree *DT,
280 bool UseInstrInfo) {
281 return ::isKnownNonEqual(V1, V2,
282 Query(DL, AC, safeCxtI(V1, safeCxtI(V2, CxtI)), DT,
283 UseInstrInfo, /*ORE=*/nullptr));
James Molloy1d88d6f2015-10-22 13:18:42 +0000284}
285
Pete Cooper35b00d52016-08-13 01:05:32 +0000286static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000287 const Query &Q);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000288
Pete Cooper35b00d52016-08-13 01:05:32 +0000289bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
Florian Hahn19f9e322018-08-17 14:39:04 +0000290 const DataLayout &DL, unsigned Depth,
291 AssumptionCache *AC, const Instruction *CxtI,
292 const DominatorTree *DT, bool UseInstrInfo) {
293 return ::MaskedValueIsZero(
294 V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000295}
296
Pete Cooper35b00d52016-08-13 01:05:32 +0000297static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
298 const Query &Q);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000299
Pete Cooper35b00d52016-08-13 01:05:32 +0000300unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000301 unsigned Depth, AssumptionCache *AC,
302 const Instruction *CxtI,
Florian Hahn19f9e322018-08-17 14:39:04 +0000303 const DominatorTree *DT, bool UseInstrInfo) {
304 return ::ComputeNumSignBits(
305 V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
Hal Finkel60db0582014-09-07 18:57:58 +0000306}
307
Craig Topper8fbb74b2017-03-24 22:12:10 +0000308static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
309 bool NSW,
Craig Topperb45eabc2017-04-26 16:39:58 +0000310 KnownBits &KnownOut, KnownBits &Known2,
Craig Topper8fbb74b2017-03-24 22:12:10 +0000311 unsigned Depth, const Query &Q) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000312 unsigned BitWidth = KnownOut.getBitWidth();
Craig Topper8fbb74b2017-03-24 22:12:10 +0000313
314 // If an initial sequence of bits in the result is not needed, the
315 // corresponding bits in the operands are not needed.
Craig Topperb45eabc2017-04-26 16:39:58 +0000316 KnownBits LHSKnown(BitWidth);
317 computeKnownBits(Op0, LHSKnown, Depth + 1, Q);
318 computeKnownBits(Op1, Known2, Depth + 1, Q);
Craig Topper8fbb74b2017-03-24 22:12:10 +0000319
Craig Topperb498a232017-08-08 16:29:35 +0000320 KnownOut = KnownBits::computeForAddSub(Add, NSW, LHSKnown, Known2);
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000321}
322
Pete Cooper35b00d52016-08-13 01:05:32 +0000323static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
Craig Topperb45eabc2017-04-26 16:39:58 +0000324 KnownBits &Known, KnownBits &Known2,
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000325 unsigned Depth, const Query &Q) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000326 unsigned BitWidth = Known.getBitWidth();
327 computeKnownBits(Op1, Known, Depth + 1, Q);
328 computeKnownBits(Op0, Known2, Depth + 1, Q);
Nick Lewyckyfa306072012-03-18 23:28:48 +0000329
330 bool isKnownNegative = false;
331 bool isKnownNonNegative = false;
332 // If the multiplication is known not to overflow, compute the sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000333 if (NSW) {
Nick Lewyckyfa306072012-03-18 23:28:48 +0000334 if (Op0 == Op1) {
335 // The product of a number with itself is non-negative.
336 isKnownNonNegative = true;
337 } else {
Craig Topperca48af32017-04-29 16:43:11 +0000338 bool isKnownNonNegativeOp1 = Known.isNonNegative();
339 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
340 bool isKnownNegativeOp1 = Known.isNegative();
341 bool isKnownNegativeOp0 = Known2.isNegative();
Nick Lewyckyfa306072012-03-18 23:28:48 +0000342 // The product of two numbers with the same sign is non-negative.
343 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
344 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
345 // The product of a negative number and a non-negative number is either
346 // negative or zero.
347 if (!isKnownNonNegative)
348 isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000349 isKnownNonZero(Op0, Depth, Q)) ||
Nick Lewyckyfa306072012-03-18 23:28:48 +0000350 (isKnownNegativeOp0 && isKnownNonNegativeOp1 &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000351 isKnownNonZero(Op1, Depth, Q));
Nick Lewyckyfa306072012-03-18 23:28:48 +0000352 }
353 }
354
Simon Dardis70dbd5f2017-12-09 23:25:57 +0000355 assert(!Known.hasConflict() && !Known2.hasConflict());
356 // Compute a conservative estimate for high known-0 bits.
Craig Topper8df66c62017-05-12 17:20:30 +0000357 unsigned LeadZ = std::max(Known.countMinLeadingZeros() +
358 Known2.countMinLeadingZeros(),
Nick Lewyckyfa306072012-03-18 23:28:48 +0000359 BitWidth) - BitWidth;
Nick Lewyckyfa306072012-03-18 23:28:48 +0000360 LeadZ = std::min(LeadZ, BitWidth);
Simon Dardis70dbd5f2017-12-09 23:25:57 +0000361
362 // The result of the bottom bits of an integer multiply can be
363 // inferred by looking at the bottom bits of both operands and
364 // multiplying them together.
365 // We can infer at least the minimum number of known trailing bits
366 // of both operands. Depending on number of trailing zeros, we can
367 // infer more bits, because (a*b) <=> ((a/m) * (b/n)) * (m*n) assuming
368 // a and b are divisible by m and n respectively.
369 // We then calculate how many of those bits are inferrable and set
370 // the output. For example, the i8 mul:
371 // a = XXXX1100 (12)
372 // b = XXXX1110 (14)
373 // We know the bottom 3 bits are zero since the first can be divided by
374 // 4 and the second by 2, thus having ((12/4) * (14/2)) * (2*4).
375 // Applying the multiplication to the trimmed arguments gets:
376 // XX11 (3)
377 // X111 (7)
378 // -------
379 // XX11
380 // XX11
381 // XX11
382 // XX11
383 // -------
384 // XXXXX01
385 // Which allows us to infer the 2 LSBs. Since we're multiplying the result
386 // by 8, the bottom 3 bits will be 0, so we can infer a total of 5 bits.
387 // The proof for this can be described as:
388 // Pre: (C1 >= 0) && (C1 < (1 << C5)) && (C2 >= 0) && (C2 < (1 << C6)) &&
389 // (C7 == (1 << (umin(countTrailingZeros(C1), C5) +
390 // umin(countTrailingZeros(C2), C6) +
391 // umin(C5 - umin(countTrailingZeros(C1), C5),
392 // C6 - umin(countTrailingZeros(C2), C6)))) - 1)
393 // %aa = shl i8 %a, C5
394 // %bb = shl i8 %b, C6
395 // %aaa = or i8 %aa, C1
396 // %bbb = or i8 %bb, C2
397 // %mul = mul i8 %aaa, %bbb
398 // %mask = and i8 %mul, C7
399 // =>
400 // %mask = i8 ((C1*C2)&C7)
401 // Where C5, C6 describe the known bits of %a, %b
402 // C1, C2 describe the known bottom bits of %a, %b.
403 // C7 describes the mask of the known bits of the result.
404 APInt Bottom0 = Known.One;
405 APInt Bottom1 = Known2.One;
406
407 // How many times we'd be able to divide each argument by 2 (shr by 1).
408 // This gives us the number of trailing zeros on the multiplication result.
409 unsigned TrailBitsKnown0 = (Known.Zero | Known.One).countTrailingOnes();
410 unsigned TrailBitsKnown1 = (Known2.Zero | Known2.One).countTrailingOnes();
411 unsigned TrailZero0 = Known.countMinTrailingZeros();
412 unsigned TrailZero1 = Known2.countMinTrailingZeros();
413 unsigned TrailZ = TrailZero0 + TrailZero1;
414
415 // Figure out the fewest known-bits operand.
416 unsigned SmallestOperand = std::min(TrailBitsKnown0 - TrailZero0,
417 TrailBitsKnown1 - TrailZero1);
418 unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ, BitWidth);
419
420 APInt BottomKnown = Bottom0.getLoBits(TrailBitsKnown0) *
421 Bottom1.getLoBits(TrailBitsKnown1);
422
Craig Topperf0aeee02017-05-05 17:36:09 +0000423 Known.resetAll();
Craig Topperb45eabc2017-04-26 16:39:58 +0000424 Known.Zero.setHighBits(LeadZ);
Simon Dardis70dbd5f2017-12-09 23:25:57 +0000425 Known.Zero |= (~BottomKnown).getLoBits(ResultBitsKnown);
426 Known.One |= BottomKnown.getLoBits(ResultBitsKnown);
Nick Lewyckyfa306072012-03-18 23:28:48 +0000427
428 // Only make use of no-wrap flags if we failed to compute the sign bit
429 // directly. This matters if the multiplication always overflows, in
430 // which case we prefer to follow the result of the direct computation,
431 // though as the program is invoking undefined behaviour we can choose
432 // whatever we like here.
Craig Topperca48af32017-04-29 16:43:11 +0000433 if (isKnownNonNegative && !Known.isNegative())
434 Known.makeNonNegative();
435 else if (isKnownNegative && !Known.isNonNegative())
436 Known.makeNegative();
Nick Lewyckyfa306072012-03-18 23:28:48 +0000437}
438
Jingyue Wu37fcb592014-06-19 16:50:16 +0000439void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
Craig Topperf42b23f2017-04-28 06:28:56 +0000440 KnownBits &Known) {
441 unsigned BitWidth = Known.getBitWidth();
Rafael Espindola53190532012-03-30 15:52:11 +0000442 unsigned NumRanges = Ranges.getNumOperands() / 2;
443 assert(NumRanges >= 1);
444
Craig Topperf42b23f2017-04-28 06:28:56 +0000445 Known.Zero.setAllBits();
446 Known.One.setAllBits();
Sanjoy Das1d1929a2015-10-28 03:20:15 +0000447
Rafael Espindola53190532012-03-30 15:52:11 +0000448 for (unsigned i = 0; i < NumRanges; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000449 ConstantInt *Lower =
450 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
451 ConstantInt *Upper =
452 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
Rafael Espindola53190532012-03-30 15:52:11 +0000453 ConstantRange Range(Lower->getValue(), Upper->getValue());
Rafael Espindola53190532012-03-30 15:52:11 +0000454
Sanjoy Das1d1929a2015-10-28 03:20:15 +0000455 // The first CommonPrefixBits of all values in Range are equal.
456 unsigned CommonPrefixBits =
457 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countLeadingZeros();
458
459 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
Craig Topperf42b23f2017-04-28 06:28:56 +0000460 Known.One &= Range.getUnsignedMax() & Mask;
461 Known.Zero &= ~Range.getUnsignedMax() & Mask;
Sanjoy Das1d1929a2015-10-28 03:20:15 +0000462 }
Rafael Espindola53190532012-03-30 15:52:11 +0000463}
Jay Foad5a29c362014-05-15 12:12:55 +0000464
Pete Cooperfa7ae4f2016-08-11 22:23:07 +0000465static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
Hal Finkel60db0582014-09-07 18:57:58 +0000466 SmallVector<const Value *, 16> WorkSet(1, I);
467 SmallPtrSet<const Value *, 32> Visited;
468 SmallPtrSet<const Value *, 16> EphValues;
469
Hal Finkelf2199b22015-10-23 20:37:08 +0000470 // The instruction defining an assumption's condition itself is always
471 // considered ephemeral to that assumption (even if it has other
472 // non-ephemeral users). See r246696's test case for an example.
David Majnemer0a16c222016-08-11 21:15:00 +0000473 if (is_contained(I->operands(), E))
Hal Finkelf2199b22015-10-23 20:37:08 +0000474 return true;
475
Hal Finkel60db0582014-09-07 18:57:58 +0000476 while (!WorkSet.empty()) {
477 const Value *V = WorkSet.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +0000478 if (!Visited.insert(V).second)
Hal Finkel60db0582014-09-07 18:57:58 +0000479 continue;
480
481 // If all uses of this value are ephemeral, then so is this value.
Eugene Zelenko75075ef2017-09-01 21:37:29 +0000482 if (llvm::all_of(V->users(), [&](const User *U) {
483 return EphValues.count(U);
484 })) {
Hal Finkel60db0582014-09-07 18:57:58 +0000485 if (V == E)
486 return true;
487
Hal Finkelb03dd4b2017-08-14 17:11:43 +0000488 if (V == I || isSafeToSpeculativelyExecute(V)) {
489 EphValues.insert(V);
490 if (const User *U = dyn_cast<User>(V))
491 for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
492 J != JE; ++J)
493 WorkSet.push_back(*J);
494 }
Hal Finkel60db0582014-09-07 18:57:58 +0000495 }
496 }
497
498 return false;
499}
500
501// Is this an intrinsic that cannot be speculated but also cannot trap?
Haicheng Wua4461512017-12-15 14:34:41 +0000502bool llvm::isAssumeLikeIntrinsic(const Instruction *I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000503 if (const CallInst *CI = dyn_cast<CallInst>(I))
504 if (Function *F = CI->getCalledFunction())
505 switch (F->getIntrinsicID()) {
506 default: break;
507 // FIXME: This list is repeated from NoTTI::getIntrinsicCost.
508 case Intrinsic::assume:
Dan Gohman2c74fe92017-11-08 21:59:51 +0000509 case Intrinsic::sideeffect:
Hal Finkel60db0582014-09-07 18:57:58 +0000510 case Intrinsic::dbg_declare:
511 case Intrinsic::dbg_value:
Shiva Chen2c864552018-05-09 02:40:45 +0000512 case Intrinsic::dbg_label:
Hal Finkel60db0582014-09-07 18:57:58 +0000513 case Intrinsic::invariant_start:
514 case Intrinsic::invariant_end:
515 case Intrinsic::lifetime_start:
516 case Intrinsic::lifetime_end:
517 case Intrinsic::objectsize:
518 case Intrinsic::ptr_annotation:
519 case Intrinsic::var_annotation:
520 return true;
521 }
522
523 return false;
524}
525
Pete Cooperfa7ae4f2016-08-11 22:23:07 +0000526bool llvm::isValidAssumeForContext(const Instruction *Inv,
527 const Instruction *CxtI,
528 const DominatorTree *DT) {
Hal Finkel60db0582014-09-07 18:57:58 +0000529 // There are two restrictions on the use of an assume:
530 // 1. The assume must dominate the context (or the control flow must
531 // reach the assume whenever it reaches the context).
532 // 2. The context must not be in the assume's set of ephemeral values
533 // (otherwise we will use the assume to prove that the condition
534 // feeding the assume is trivially true, thus causing the removal of
535 // the assume).
536
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000537 if (DT) {
Pete Cooper54a02552016-08-12 01:00:15 +0000538 if (DT->dominates(Inv, CxtI))
Hal Finkel60db0582014-09-07 18:57:58 +0000539 return true;
Pete Cooper54a02552016-08-12 01:00:15 +0000540 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) {
541 // We don't have a DT, but this trivially dominates.
542 return true;
Hal Finkel60db0582014-09-07 18:57:58 +0000543 }
544
Pete Cooper54a02552016-08-12 01:00:15 +0000545 // With or without a DT, the only remaining case we will check is if the
546 // instructions are in the same BB. Give up if that is not the case.
547 if (Inv->getParent() != CxtI->getParent())
548 return false;
549
Vedant Kumard3196742018-02-28 19:08:52 +0000550 // If we have a dom tree, then we now know that the assume doesn't dominate
Pete Cooper54a02552016-08-12 01:00:15 +0000551 // the other instruction. If we don't have a dom tree then we can check if
552 // the assume is first in the BB.
553 if (!DT) {
Hal Finkel60db0582014-09-07 18:57:58 +0000554 // Search forward from the assume until we reach the context (or the end
555 // of the block); the common case is that the assume will come first.
Pete Cooperfa7ae4f2016-08-11 22:23:07 +0000556 for (auto I = std::next(BasicBlock::const_iterator(Inv)),
Hal Finkel60db0582014-09-07 18:57:58 +0000557 IE = Inv->getParent()->end(); I != IE; ++I)
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000558 if (&*I == CxtI)
Hal Finkel60db0582014-09-07 18:57:58 +0000559 return true;
Hal Finkel60db0582014-09-07 18:57:58 +0000560 }
561
Pete Cooper54a02552016-08-12 01:00:15 +0000562 // The context comes first, but they're both in the same block. Make sure
563 // there is nothing in between that might interrupt the control flow.
564 for (BasicBlock::const_iterator I =
565 std::next(BasicBlock::const_iterator(CxtI)), IE(Inv);
566 I != IE; ++I)
567 if (!isSafeToSpeculativelyExecute(&*I) && !isAssumeLikeIntrinsic(&*I))
568 return false;
569
570 return !isEphemeralValueOf(Inv, CxtI);
Hal Finkel60db0582014-09-07 18:57:58 +0000571}
572
Craig Topperb45eabc2017-04-26 16:39:58 +0000573static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
574 unsigned Depth, const Query &Q) {
Hal Finkel60db0582014-09-07 18:57:58 +0000575 // Use of assumptions is context-sensitive. If we don't have a context, we
576 // cannot use them!
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000577 if (!Q.AC || !Q.CxtI)
Hal Finkel60db0582014-09-07 18:57:58 +0000578 return;
579
Craig Topperb45eabc2017-04-26 16:39:58 +0000580 unsigned BitWidth = Known.getBitWidth();
Hal Finkel60db0582014-09-07 18:57:58 +0000581
Hal Finkel8a9a7832017-01-11 13:24:24 +0000582 // Note that the patterns below need to be kept in sync with the code
583 // in AssumptionCache::updateAffectedValues.
584
585 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000586 if (!AssumeVH)
Chandler Carruth66b31302015-01-04 12:03:27 +0000587 continue;
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000588 CallInst *I = cast<CallInst>(AssumeVH);
589 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
590 "Got assumption for the wrong function!");
591 if (Q.isExcluded(I))
Hal Finkel60db0582014-09-07 18:57:58 +0000592 continue;
593
Vedant Kumard3196742018-02-28 19:08:52 +0000594 // Warning: This loop can end up being somewhat performance sensitive.
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000595 // We're running this loop for once for each value queried resulting in a
596 // runtime of ~O(#assumes * #values).
Philip Reames00d3b272014-11-24 23:44:28 +0000597
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000598 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
599 "must be an assume intrinsic");
600
601 Value *Arg = I->getArgOperand(0);
602
603 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Hal Finkel60db0582014-09-07 18:57:58 +0000604 assert(BitWidth == 1 && "assume operand is not i1?");
Craig Topperf0aeee02017-05-05 17:36:09 +0000605 Known.setAllOnes();
Hal Finkel60db0582014-09-07 18:57:58 +0000606 return;
607 }
Sanjay Patel96669962017-01-17 18:15:49 +0000608 if (match(Arg, m_Not(m_Specific(V))) &&
609 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
610 assert(BitWidth == 1 && "assume operand is not i1?");
Craig Topperf0aeee02017-05-05 17:36:09 +0000611 Known.setAllZero();
Sanjay Patel96669962017-01-17 18:15:49 +0000612 return;
613 }
Hal Finkel60db0582014-09-07 18:57:58 +0000614
David Majnemer9b609752014-12-12 23:59:29 +0000615 // The remaining tests are all recursive, so bail out if we hit the limit.
616 if (Depth == MaxDepth)
617 continue;
618
Hal Finkel60db0582014-09-07 18:57:58 +0000619 Value *A, *B;
620 auto m_V = m_CombineOr(m_Specific(V),
621 m_CombineOr(m_PtrToInt(m_Specific(V)),
622 m_BitCast(m_Specific(V))));
623
624 CmpInst::Predicate Pred;
Igor Laevskycec8f472017-12-05 12:18:15 +0000625 uint64_t C;
Hal Finkel60db0582014-09-07 18:57:58 +0000626 // assume(v = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000627 if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000628 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000629 KnownBits RHSKnown(BitWidth);
630 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
631 Known.Zero |= RHSKnown.Zero;
632 Known.One |= RHSKnown.One;
Hal Finkel60db0582014-09-07 18:57:58 +0000633 // assume(v & b = a)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000634 } else if (match(Arg,
635 m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000636 Pred == ICmpInst::ICMP_EQ &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000637 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000638 KnownBits RHSKnown(BitWidth);
639 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
640 KnownBits MaskKnown(BitWidth);
641 computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I));
Hal Finkel60db0582014-09-07 18:57:58 +0000642
643 // For those bits in the mask that are known to be one, we can propagate
644 // known bits from the RHS to V.
Craig Topperb45eabc2017-04-26 16:39:58 +0000645 Known.Zero |= RHSKnown.Zero & MaskKnown.One;
646 Known.One |= RHSKnown.One & MaskKnown.One;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000647 // assume(~(v & b) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000648 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))),
649 m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000650 Pred == ICmpInst::ICMP_EQ &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000651 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000652 KnownBits RHSKnown(BitWidth);
653 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
654 KnownBits MaskKnown(BitWidth);
655 computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000656
657 // For those bits in the mask that are known to be one, we can propagate
658 // inverted known bits from the RHS to V.
Craig Topperb45eabc2017-04-26 16:39:58 +0000659 Known.Zero |= RHSKnown.One & MaskKnown.One;
660 Known.One |= RHSKnown.Zero & MaskKnown.One;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000661 // assume(v | b = a)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000662 } else if (match(Arg,
663 m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000664 Pred == ICmpInst::ICMP_EQ &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000665 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000666 KnownBits RHSKnown(BitWidth);
667 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
668 KnownBits BKnown(BitWidth);
669 computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000670
671 // For those bits in B that are known to be zero, we can propagate known
672 // bits from the RHS to V.
Craig Topperb45eabc2017-04-26 16:39:58 +0000673 Known.Zero |= RHSKnown.Zero & BKnown.Zero;
674 Known.One |= RHSKnown.One & BKnown.Zero;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000675 // assume(~(v | b) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000676 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))),
677 m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000678 Pred == ICmpInst::ICMP_EQ &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000679 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000680 KnownBits RHSKnown(BitWidth);
681 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
682 KnownBits BKnown(BitWidth);
683 computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000684
685 // For those bits in B that are known to be zero, we can propagate
686 // inverted known bits from the RHS to V.
Craig Topperb45eabc2017-04-26 16:39:58 +0000687 Known.Zero |= RHSKnown.One & BKnown.Zero;
688 Known.One |= RHSKnown.Zero & BKnown.Zero;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000689 // assume(v ^ b = a)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000690 } else if (match(Arg,
691 m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000692 Pred == ICmpInst::ICMP_EQ &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000693 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000694 KnownBits RHSKnown(BitWidth);
695 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
696 KnownBits BKnown(BitWidth);
697 computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000698
699 // For those bits in B that are known to be zero, we can propagate known
700 // bits from the RHS to V. For those bits in B that are known to be one,
701 // we can propagate inverted known bits from the RHS to V.
Craig Topperb45eabc2017-04-26 16:39:58 +0000702 Known.Zero |= RHSKnown.Zero & BKnown.Zero;
703 Known.One |= RHSKnown.One & BKnown.Zero;
704 Known.Zero |= RHSKnown.One & BKnown.One;
705 Known.One |= RHSKnown.Zero & BKnown.One;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000706 // assume(~(v ^ b) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000707 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))),
708 m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000709 Pred == ICmpInst::ICMP_EQ &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000710 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000711 KnownBits RHSKnown(BitWidth);
712 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
713 KnownBits BKnown(BitWidth);
714 computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000715
716 // For those bits in B that are known to be zero, we can propagate
717 // inverted known bits from the RHS to V. For those bits in B that are
718 // known to be one, we can propagate known bits from the RHS to V.
Craig Topperb45eabc2017-04-26 16:39:58 +0000719 Known.Zero |= RHSKnown.One & BKnown.Zero;
720 Known.One |= RHSKnown.Zero & BKnown.Zero;
721 Known.Zero |= RHSKnown.Zero & BKnown.One;
722 Known.One |= RHSKnown.One & BKnown.One;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000723 // assume(v << c = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000724 } else if (match(Arg, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
725 m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000726 Pred == ICmpInst::ICMP_EQ &&
Igor Laevskycec8f472017-12-05 12:18:15 +0000727 isValidAssumeForContext(I, Q.CxtI, Q.DT) &&
728 C < BitWidth) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000729 KnownBits RHSKnown(BitWidth);
730 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000731 // For those bits in RHS that are known, we can propagate them to known
732 // bits in V shifted to the right by C.
Igor Laevskycec8f472017-12-05 12:18:15 +0000733 RHSKnown.Zero.lshrInPlace(C);
Craig Topperb45eabc2017-04-26 16:39:58 +0000734 Known.Zero |= RHSKnown.Zero;
Igor Laevskycec8f472017-12-05 12:18:15 +0000735 RHSKnown.One.lshrInPlace(C);
Craig Topperb45eabc2017-04-26 16:39:58 +0000736 Known.One |= RHSKnown.One;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000737 // assume(~(v << c) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000738 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))),
739 m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000740 Pred == ICmpInst::ICMP_EQ &&
Igor Laevskycec8f472017-12-05 12:18:15 +0000741 isValidAssumeForContext(I, Q.CxtI, Q.DT) &&
742 C < BitWidth) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000743 KnownBits RHSKnown(BitWidth);
744 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000745 // For those bits in RHS that are known, we can propagate them inverted
746 // to known bits in V shifted to the right by C.
Igor Laevskycec8f472017-12-05 12:18:15 +0000747 RHSKnown.One.lshrInPlace(C);
Craig Topperb45eabc2017-04-26 16:39:58 +0000748 Known.Zero |= RHSKnown.One;
Igor Laevskycec8f472017-12-05 12:18:15 +0000749 RHSKnown.Zero.lshrInPlace(C);
Craig Topperb45eabc2017-04-26 16:39:58 +0000750 Known.One |= RHSKnown.Zero;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000751 // assume(v >> c = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000752 } else if (match(Arg,
Craig Topper7b66ffe2017-06-24 06:24:04 +0000753 m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)),
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000754 m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000755 Pred == ICmpInst::ICMP_EQ &&
Igor Laevskycec8f472017-12-05 12:18:15 +0000756 isValidAssumeForContext(I, Q.CxtI, Q.DT) &&
757 C < BitWidth) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000758 KnownBits RHSKnown(BitWidth);
759 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000760 // For those bits in RHS that are known, we can propagate them to known
761 // bits in V shifted to the right by C.
Igor Laevskycec8f472017-12-05 12:18:15 +0000762 Known.Zero |= RHSKnown.Zero << C;
763 Known.One |= RHSKnown.One << C;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000764 // assume(~(v >> c) = a)
Craig Topper7b66ffe2017-06-24 06:24:04 +0000765 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))),
Philip Reames00d3b272014-11-24 23:44:28 +0000766 m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000767 Pred == ICmpInst::ICMP_EQ &&
Igor Laevskycec8f472017-12-05 12:18:15 +0000768 isValidAssumeForContext(I, Q.CxtI, Q.DT) &&
769 C < BitWidth) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000770 KnownBits RHSKnown(BitWidth);
771 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000772 // For those bits in RHS that are known, we can propagate them inverted
773 // to known bits in V shifted to the right by C.
Igor Laevskycec8f472017-12-05 12:18:15 +0000774 Known.Zero |= RHSKnown.One << C;
775 Known.One |= RHSKnown.Zero << C;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000776 // assume(v >=_s c) where c is non-negative
Philip Reames00d3b272014-11-24 23:44:28 +0000777 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000778 Pred == ICmpInst::ICMP_SGE &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000779 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000780 KnownBits RHSKnown(BitWidth);
781 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000782
Craig Topperca48af32017-04-29 16:43:11 +0000783 if (RHSKnown.isNonNegative()) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000784 // We know that the sign bit is zero.
Craig Topperca48af32017-04-29 16:43:11 +0000785 Known.makeNonNegative();
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000786 }
787 // assume(v >_s c) where c is at least -1.
Philip Reames00d3b272014-11-24 23:44:28 +0000788 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000789 Pred == ICmpInst::ICMP_SGT &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000790 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000791 KnownBits RHSKnown(BitWidth);
792 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000793
Craig Topperf0aeee02017-05-05 17:36:09 +0000794 if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000795 // We know that the sign bit is zero.
Craig Topperca48af32017-04-29 16:43:11 +0000796 Known.makeNonNegative();
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000797 }
798 // assume(v <=_s c) where c is negative
Philip Reames00d3b272014-11-24 23:44:28 +0000799 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000800 Pred == ICmpInst::ICMP_SLE &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000801 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000802 KnownBits RHSKnown(BitWidth);
803 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000804
Craig Topperca48af32017-04-29 16:43:11 +0000805 if (RHSKnown.isNegative()) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000806 // We know that the sign bit is one.
Craig Topperca48af32017-04-29 16:43:11 +0000807 Known.makeNegative();
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000808 }
809 // assume(v <_s c) where c is non-positive
Philip Reames00d3b272014-11-24 23:44:28 +0000810 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000811 Pred == ICmpInst::ICMP_SLT &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000812 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000813 KnownBits RHSKnown(BitWidth);
814 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000815
Craig Topperf0aeee02017-05-05 17:36:09 +0000816 if (RHSKnown.isZero() || RHSKnown.isNegative()) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000817 // We know that the sign bit is one.
Craig Topperca48af32017-04-29 16:43:11 +0000818 Known.makeNegative();
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000819 }
820 // assume(v <=_u c)
Philip Reames00d3b272014-11-24 23:44:28 +0000821 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000822 Pred == ICmpInst::ICMP_ULE &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000823 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000824 KnownBits RHSKnown(BitWidth);
825 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000826
827 // Whatever high bits in c are zero are known to be zero.
Craig Topper8df66c62017-05-12 17:20:30 +0000828 Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
829 // assume(v <_u c)
Philip Reames00d3b272014-11-24 23:44:28 +0000830 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000831 Pred == ICmpInst::ICMP_ULT &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000832 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000833 KnownBits RHSKnown(BitWidth);
834 computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000835
Sanjay Patela60aec12018-02-08 14:52:40 +0000836 // If the RHS is known zero, then this assumption must be wrong (nothing
837 // is unsigned less than zero). Signal a conflict and get out of here.
838 if (RHSKnown.isZero()) {
839 Known.Zero.setAllBits();
840 Known.One.setAllBits();
841 break;
842 }
843
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000844 // Whatever high bits in c are zero are known to be zero (if c is a power
845 // of 2, then one more).
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000846 if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I)))
Craig Topper8df66c62017-05-12 17:20:30 +0000847 Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros() + 1);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000848 else
Craig Topper8df66c62017-05-12 17:20:30 +0000849 Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
Hal Finkel60db0582014-09-07 18:57:58 +0000850 }
851 }
Sanjay Patel25f6d712017-02-01 15:41:32 +0000852
853 // If assumptions conflict with each other or previous known bits, then we
Sanjay Patel54656ca2017-02-06 18:26:06 +0000854 // have a logical fallacy. It's possible that the assumption is not reachable,
855 // so this isn't a real bug. On the other hand, the program may have undefined
856 // behavior, or we might have a bug in the compiler. We can't assert/crash, so
857 // clear out the known bits, try to warn the user, and hope for the best.
Craig Topperb45eabc2017-04-26 16:39:58 +0000858 if (Known.Zero.intersects(Known.One)) {
Craig Topperf0aeee02017-05-05 17:36:09 +0000859 Known.resetAll();
Sanjay Patel54656ca2017-02-06 18:26:06 +0000860
Vivek Pandya95906582017-10-11 17:12:59 +0000861 if (Q.ORE)
862 Q.ORE->emit([&]() {
863 auto *CxtI = const_cast<Instruction *>(Q.CxtI);
864 return OptimizationRemarkAnalysis("value-tracking", "BadAssumption",
865 CxtI)
866 << "Detected conflicting code assumptions. Program may "
867 "have undefined behavior, or compiler may have "
868 "internal error.";
869 });
Sanjay Patel25f6d712017-02-01 15:41:32 +0000870 }
Hal Finkel60db0582014-09-07 18:57:58 +0000871}
872
Sanjay Patelb7d12382017-10-16 14:46:37 +0000873/// Compute known bits from a shift operator, including those with a
874/// non-constant shift amount. Known is the output of this function. Known2 is a
875/// pre-allocated temporary with the same bit width as Known. KZF and KOF are
Vedant Kumard3196742018-02-28 19:08:52 +0000876/// operator-specific functions that, given the known-zero or known-one bits
Sanjay Patelb7d12382017-10-16 14:46:37 +0000877/// respectively, and a shift amount, compute the implied known-zero or
878/// known-one bits of the shift operator's result respectively for that shift
879/// amount. The results from calling KZF and KOF are conservatively combined for
880/// all permitted shift amounts.
David Majnemer54690dc2016-08-23 20:52:00 +0000881static void computeKnownBitsFromShiftOperator(
Craig Topperb45eabc2017-04-26 16:39:58 +0000882 const Operator *I, KnownBits &Known, KnownBits &Known2,
883 unsigned Depth, const Query &Q,
Sam McCalld0d43e62017-12-04 12:51:49 +0000884 function_ref<APInt(const APInt &, unsigned)> KZF,
885 function_ref<APInt(const APInt &, unsigned)> KOF) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000886 unsigned BitWidth = Known.getBitWidth();
Hal Finkelf2199b22015-10-23 20:37:08 +0000887
888 if (auto *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
889 unsigned ShiftAmt = SA->getLimitedValue(BitWidth-1);
890
Craig Topperb45eabc2017-04-26 16:39:58 +0000891 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
Sam McCalld0d43e62017-12-04 12:51:49 +0000892 Known.Zero = KZF(Known.Zero, ShiftAmt);
893 Known.One = KOF(Known.One, ShiftAmt);
Sanjay Patele272be72017-10-12 17:31:46 +0000894 // If the known bits conflict, this must be an overflowing left shift, so
895 // the shift result is poison. We can return anything we want. Choose 0 for
896 // the best folding opportunity.
897 if (Known.hasConflict())
898 Known.setAllZero();
Evgeny Stupachenkod7f9c352016-08-24 23:01:33 +0000899
Hal Finkelf2199b22015-10-23 20:37:08 +0000900 return;
901 }
902
Craig Topperb45eabc2017-04-26 16:39:58 +0000903 computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
Hal Finkelf2199b22015-10-23 20:37:08 +0000904
Sanjay Patele272be72017-10-12 17:31:46 +0000905 // If the shift amount could be greater than or equal to the bit-width of the
906 // LHS, the value could be poison, but bail out because the check below is
907 // expensive. TODO: Should we just carry on?
Craig Topperb45eabc2017-04-26 16:39:58 +0000908 if ((~Known.Zero).uge(BitWidth)) {
Craig Topperf0aeee02017-05-05 17:36:09 +0000909 Known.resetAll();
Oliver Stannard06204112017-03-14 10:13:17 +0000910 return;
911 }
912
Craig Topperb45eabc2017-04-26 16:39:58 +0000913 // Note: We cannot use Known.Zero.getLimitedValue() here, because if
Hal Finkelf2199b22015-10-23 20:37:08 +0000914 // BitWidth > 64 and any upper bits are known, we'll end up returning the
915 // limit value (which implies all bits are known).
Craig Topperb45eabc2017-04-26 16:39:58 +0000916 uint64_t ShiftAmtKZ = Known.Zero.zextOrTrunc(64).getZExtValue();
917 uint64_t ShiftAmtKO = Known.One.zextOrTrunc(64).getZExtValue();
Hal Finkelf2199b22015-10-23 20:37:08 +0000918
919 // It would be more-clearly correct to use the two temporaries for this
920 // calculation. Reusing the APInts here to prevent unnecessary allocations.
Craig Topperf0aeee02017-05-05 17:36:09 +0000921 Known.resetAll();
Hal Finkelf2199b22015-10-23 20:37:08 +0000922
James Molloy493e57d2015-10-26 14:10:46 +0000923 // If we know the shifter operand is nonzero, we can sometimes infer more
924 // known bits. However this is expensive to compute, so be lazy about it and
925 // only compute it when absolutely necessary.
926 Optional<bool> ShifterOperandIsNonZero;
927
Hal Finkelf2199b22015-10-23 20:37:08 +0000928 // Early exit if we can't constrain any well-defined shift amount.
Craig Topperf93b7b12017-06-14 17:04:59 +0000929 if (!(ShiftAmtKZ & (PowerOf2Ceil(BitWidth) - 1)) &&
930 !(ShiftAmtKO & (PowerOf2Ceil(BitWidth) - 1))) {
Sanjay Patelb7d12382017-10-16 14:46:37 +0000931 ShifterOperandIsNonZero = isKnownNonZero(I->getOperand(1), Depth + 1, Q);
James Molloy493e57d2015-10-26 14:10:46 +0000932 if (!*ShifterOperandIsNonZero)
933 return;
934 }
Hal Finkelf2199b22015-10-23 20:37:08 +0000935
Craig Topperb45eabc2017-04-26 16:39:58 +0000936 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
Hal Finkelf2199b22015-10-23 20:37:08 +0000937
Craig Topperb45eabc2017-04-26 16:39:58 +0000938 Known.Zero.setAllBits();
939 Known.One.setAllBits();
Hal Finkelf2199b22015-10-23 20:37:08 +0000940 for (unsigned ShiftAmt = 0; ShiftAmt < BitWidth; ++ShiftAmt) {
941 // Combine the shifted known input bits only for those shift amounts
942 // compatible with its known constraints.
943 if ((ShiftAmt & ~ShiftAmtKZ) != ShiftAmt)
944 continue;
945 if ((ShiftAmt | ShiftAmtKO) != ShiftAmt)
946 continue;
James Molloy493e57d2015-10-26 14:10:46 +0000947 // If we know the shifter is nonzero, we may be able to infer more known
948 // bits. This check is sunk down as far as possible to avoid the expensive
949 // call to isKnownNonZero if the cheaper checks above fail.
950 if (ShiftAmt == 0) {
951 if (!ShifterOperandIsNonZero.hasValue())
952 ShifterOperandIsNonZero =
Matthias Braunfeb81bc2016-01-15 22:22:04 +0000953 isKnownNonZero(I->getOperand(1), Depth + 1, Q);
James Molloy493e57d2015-10-26 14:10:46 +0000954 if (*ShifterOperandIsNonZero)
955 continue;
956 }
Hal Finkelf2199b22015-10-23 20:37:08 +0000957
Sam McCalld0d43e62017-12-04 12:51:49 +0000958 Known.Zero &= KZF(Known2.Zero, ShiftAmt);
959 Known.One &= KOF(Known2.One, ShiftAmt);
Hal Finkelf2199b22015-10-23 20:37:08 +0000960 }
961
Sanjay Patele272be72017-10-12 17:31:46 +0000962 // If the known bits conflict, the result is poison. Return a 0 and hope the
963 // caller can further optimize that.
964 if (Known.hasConflict())
965 Known.setAllZero();
Hal Finkelf2199b22015-10-23 20:37:08 +0000966}
967
Craig Topperb45eabc2017-04-26 16:39:58 +0000968static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
969 unsigned Depth, const Query &Q) {
970 unsigned BitWidth = Known.getBitWidth();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000971
Craig Topperb45eabc2017-04-26 16:39:58 +0000972 KnownBits Known2(Known);
Dan Gohman80ca01c2009-07-17 20:47:02 +0000973 switch (I->getOpcode()) {
Chris Lattner965c7692008-06-02 01:18:21 +0000974 default: break;
Rafael Espindola53190532012-03-30 15:52:11 +0000975 case Instruction::Load:
Florian Hahn19f9e322018-08-17 14:39:04 +0000976 if (MDNode *MD =
977 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
Craig Topperf42b23f2017-04-28 06:28:56 +0000978 computeKnownBitsFromRangeMetadata(*MD, Known);
Jay Foad5a29c362014-05-15 12:12:55 +0000979 break;
Chris Lattner965c7692008-06-02 01:18:21 +0000980 case Instruction::And: {
981 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topperb45eabc2017-04-26 16:39:58 +0000982 computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
983 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +0000984
Chris Lattner965c7692008-06-02 01:18:21 +0000985 // Output known-1 bits are only known if set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000986 Known.One &= Known2.One;
Chris Lattner965c7692008-06-02 01:18:21 +0000987 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000988 Known.Zero |= Known2.Zero;
Philip Reames2d858742015-11-10 18:46:14 +0000989
990 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
991 // here we handle the more general case of adding any odd number by
992 // matching the form add(x, add(x, y)) where y is odd.
993 // TODO: This could be generalized to clearing any bit set in y where the
994 // following bit is known to be unset in y.
Roman Lebedev6959b8e2018-04-27 21:23:20 +0000995 Value *X = nullptr, *Y = nullptr;
Craig Topperb45eabc2017-04-26 16:39:58 +0000996 if (!Known.Zero[0] && !Known.One[0] &&
Roman Lebedev6959b8e2018-04-27 21:23:20 +0000997 match(I, m_c_BinOp(m_Value(X), m_Add(m_Deferred(X), m_Value(Y))))) {
Craig Topperf0aeee02017-05-05 17:36:09 +0000998 Known2.resetAll();
Craig Topperb45eabc2017-04-26 16:39:58 +0000999 computeKnownBits(Y, Known2, Depth + 1, Q);
Craig Topper8df66c62017-05-12 17:20:30 +00001000 if (Known2.countMinTrailingOnes() > 0)
Craig Topperb45eabc2017-04-26 16:39:58 +00001001 Known.Zero.setBit(0);
Philip Reames2d858742015-11-10 18:46:14 +00001002 }
Jay Foad5a29c362014-05-15 12:12:55 +00001003 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001004 }
Eugene Zelenko75075ef2017-09-01 21:37:29 +00001005 case Instruction::Or:
Craig Topperb45eabc2017-04-26 16:39:58 +00001006 computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
1007 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00001008
Chris Lattner965c7692008-06-02 01:18:21 +00001009 // Output known-0 bits are only known if clear in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +00001010 Known.Zero &= Known2.Zero;
Chris Lattner965c7692008-06-02 01:18:21 +00001011 // Output known-1 are known to be set if set in either the LHS | RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +00001012 Known.One |= Known2.One;
Jay Foad5a29c362014-05-15 12:12:55 +00001013 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001014 case Instruction::Xor: {
Craig Topperb45eabc2017-04-26 16:39:58 +00001015 computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
1016 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00001017
Chris Lattner965c7692008-06-02 01:18:21 +00001018 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +00001019 APInt KnownZeroOut = (Known.Zero & Known2.Zero) | (Known.One & Known2.One);
Chris Lattner965c7692008-06-02 01:18:21 +00001020 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +00001021 Known.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero);
1022 Known.Zero = std::move(KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00001023 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001024 }
1025 case Instruction::Mul: {
Florian Hahn19f9e322018-08-17 14:39:04 +00001026 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
Craig Topperb45eabc2017-04-26 16:39:58 +00001027 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, Known,
1028 Known2, Depth, Q);
Nick Lewyckyfa306072012-03-18 23:28:48 +00001029 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001030 }
1031 case Instruction::UDiv: {
1032 // For the purposes of computing leading zeros we can conservatively
1033 // treat a udiv as a logical right shift by the power of 2 known to
1034 // be less than the denominator.
Craig Topperb45eabc2017-04-26 16:39:58 +00001035 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
Craig Topper8df66c62017-05-12 17:20:30 +00001036 unsigned LeadZ = Known2.countMinLeadingZeros();
Chris Lattner965c7692008-06-02 01:18:21 +00001037
Craig Topperf0aeee02017-05-05 17:36:09 +00001038 Known2.resetAll();
Craig Topperb45eabc2017-04-26 16:39:58 +00001039 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
Craig Topper8df66c62017-05-12 17:20:30 +00001040 unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros();
1041 if (RHSMaxLeadingZeros != BitWidth)
1042 LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);
Chris Lattner965c7692008-06-02 01:18:21 +00001043
Craig Topperb45eabc2017-04-26 16:39:58 +00001044 Known.Zero.setHighBits(LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001045 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001046 }
David Majnemera19d0f22016-08-06 08:16:00 +00001047 case Instruction::Select: {
Craig Toppere953dec2017-04-13 20:39:37 +00001048 const Value *LHS, *RHS;
David Majnemera19d0f22016-08-06 08:16:00 +00001049 SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor;
1050 if (SelectPatternResult::isMinOrMax(SPF)) {
Craig Topperb45eabc2017-04-26 16:39:58 +00001051 computeKnownBits(RHS, Known, Depth + 1, Q);
1052 computeKnownBits(LHS, Known2, Depth + 1, Q);
David Majnemera19d0f22016-08-06 08:16:00 +00001053 } else {
Craig Topperb45eabc2017-04-26 16:39:58 +00001054 computeKnownBits(I->getOperand(2), Known, Depth + 1, Q);
1055 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
David Majnemera19d0f22016-08-06 08:16:00 +00001056 }
1057
1058 unsigned MaxHighOnes = 0;
1059 unsigned MaxHighZeros = 0;
1060 if (SPF == SPF_SMAX) {
1061 // If both sides are negative, the result is negative.
Craig Topperca48af32017-04-29 16:43:11 +00001062 if (Known.isNegative() && Known2.isNegative())
David Majnemera19d0f22016-08-06 08:16:00 +00001063 // We can derive a lower bound on the result by taking the max of the
1064 // leading one bits.
Craig Topper8df66c62017-05-12 17:20:30 +00001065 MaxHighOnes =
1066 std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes());
David Majnemera19d0f22016-08-06 08:16:00 +00001067 // If either side is non-negative, the result is non-negative.
Craig Topperca48af32017-04-29 16:43:11 +00001068 else if (Known.isNonNegative() || Known2.isNonNegative())
David Majnemera19d0f22016-08-06 08:16:00 +00001069 MaxHighZeros = 1;
1070 } else if (SPF == SPF_SMIN) {
1071 // If both sides are non-negative, the result is non-negative.
Craig Topperca48af32017-04-29 16:43:11 +00001072 if (Known.isNonNegative() && Known2.isNonNegative())
David Majnemera19d0f22016-08-06 08:16:00 +00001073 // We can derive an upper bound on the result by taking the max of the
1074 // leading zero bits.
Craig Topper8df66c62017-05-12 17:20:30 +00001075 MaxHighZeros = std::max(Known.countMinLeadingZeros(),
1076 Known2.countMinLeadingZeros());
David Majnemera19d0f22016-08-06 08:16:00 +00001077 // If either side is negative, the result is negative.
Craig Topperca48af32017-04-29 16:43:11 +00001078 else if (Known.isNegative() || Known2.isNegative())
David Majnemera19d0f22016-08-06 08:16:00 +00001079 MaxHighOnes = 1;
1080 } else if (SPF == SPF_UMAX) {
1081 // We can derive a lower bound on the result by taking the max of the
1082 // leading one bits.
1083 MaxHighOnes =
Craig Topper8df66c62017-05-12 17:20:30 +00001084 std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes());
David Majnemera19d0f22016-08-06 08:16:00 +00001085 } else if (SPF == SPF_UMIN) {
1086 // We can derive an upper bound on the result by taking the max of the
1087 // leading zero bits.
1088 MaxHighZeros =
Craig Topper8df66c62017-05-12 17:20:30 +00001089 std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
Craig Topper8f77dca2018-05-25 19:18:09 +00001090 } else if (SPF == SPF_ABS) {
1091 // RHS from matchSelectPattern returns the negation part of abs pattern.
1092 // If the negate has an NSW flag we can assume the sign bit of the result
1093 // will be 0 because that makes abs(INT_MIN) undefined.
Florian Hahn19f9e322018-08-17 14:39:04 +00001094 if (Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
Craig Topper8f77dca2018-05-25 19:18:09 +00001095 MaxHighZeros = 1;
David Majnemera19d0f22016-08-06 08:16:00 +00001096 }
1097
Chris Lattner965c7692008-06-02 01:18:21 +00001098 // Only known if known in both the LHS and RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +00001099 Known.One &= Known2.One;
1100 Known.Zero &= Known2.Zero;
David Majnemera19d0f22016-08-06 08:16:00 +00001101 if (MaxHighOnes > 0)
Craig Topperb45eabc2017-04-26 16:39:58 +00001102 Known.One.setHighBits(MaxHighOnes);
David Majnemera19d0f22016-08-06 08:16:00 +00001103 if (MaxHighZeros > 0)
Craig Topperb45eabc2017-04-26 16:39:58 +00001104 Known.Zero.setHighBits(MaxHighZeros);
Jay Foad5a29c362014-05-15 12:12:55 +00001105 break;
David Majnemera19d0f22016-08-06 08:16:00 +00001106 }
Chris Lattner965c7692008-06-02 01:18:21 +00001107 case Instruction::FPTrunc:
1108 case Instruction::FPExt:
1109 case Instruction::FPToUI:
1110 case Instruction::FPToSI:
1111 case Instruction::SIToFP:
1112 case Instruction::UIToFP:
Jay Foad5a29c362014-05-15 12:12:55 +00001113 break; // Can't work with floating point.
Chris Lattner965c7692008-06-02 01:18:21 +00001114 case Instruction::PtrToInt:
1115 case Instruction::IntToPtr:
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001116 // Fall through and handle them the same as zext/trunc.
1117 LLVM_FALLTHROUGH;
Chris Lattner965c7692008-06-02 01:18:21 +00001118 case Instruction::ZExt:
1119 case Instruction::Trunc: {
Chris Lattner229907c2011-07-18 04:54:35 +00001120 Type *SrcTy = I->getOperand(0)->getType();
Nadav Rotem15198e92012-10-26 17:17:05 +00001121
Chris Lattner0cdbc7a2009-09-08 00:13:52 +00001122 unsigned SrcBitWidth;
Chris Lattner965c7692008-06-02 01:18:21 +00001123 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1124 // which fall through here.
Elena Demikhovsky945b7e52018-02-14 06:58:08 +00001125 Type *ScalarTy = SrcTy->getScalarType();
1126 SrcBitWidth = ScalarTy->isPointerTy() ?
1127 Q.DL.getIndexTypeSizeInBits(ScalarTy) :
1128 Q.DL.getTypeSizeInBits(ScalarTy);
Nadav Rotem15198e92012-10-26 17:17:05 +00001129
1130 assert(SrcBitWidth && "SrcBitWidth can't be zero");
Craig Topperd938fd12017-05-03 22:07:25 +00001131 Known = Known.zextOrTrunc(SrcBitWidth);
Craig Topperb45eabc2017-04-26 16:39:58 +00001132 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
Craig Topperd938fd12017-05-03 22:07:25 +00001133 Known = Known.zextOrTrunc(BitWidth);
Chris Lattner965c7692008-06-02 01:18:21 +00001134 // Any top bits are known to be zero.
1135 if (BitWidth > SrcBitWidth)
Craig Topperb45eabc2017-04-26 16:39:58 +00001136 Known.Zero.setBitsFrom(SrcBitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00001137 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001138 }
1139 case Instruction::BitCast: {
Chris Lattner229907c2011-07-18 04:54:35 +00001140 Type *SrcTy = I->getOperand(0)->getType();
Vedant Kumarb3091da2018-07-06 20:17:42 +00001141 if (SrcTy->isIntOrPtrTy() &&
Chris Lattneredb84072009-07-02 16:04:08 +00001142 // TODO: For now, not handling conversions like:
1143 // (bitcast i64 %x to <2 x i32>)
Duncan Sands19d0b472010-02-16 11:11:14 +00001144 !I->getType()->isVectorTy()) {
Craig Topperb45eabc2017-04-26 16:39:58 +00001145 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
Jay Foad5a29c362014-05-15 12:12:55 +00001146 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001147 }
1148 break;
1149 }
1150 case Instruction::SExt: {
1151 // Compute the bits in the result that are not present in the input.
Chris Lattner0cdbc7a2009-09-08 00:13:52 +00001152 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper1bef2c82012-12-22 19:15:35 +00001153
Craig Topperd938fd12017-05-03 22:07:25 +00001154 Known = Known.trunc(SrcBitWidth);
Craig Topperb45eabc2017-04-26 16:39:58 +00001155 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001156 // If the sign bit of the input is known set or clear, then we know the
1157 // top bits of the result.
Craig Topperd938fd12017-05-03 22:07:25 +00001158 Known = Known.sext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00001159 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001160 }
Hal Finkelf2199b22015-10-23 20:37:08 +00001161 case Instruction::Shl: {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001162 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Florian Hahn19f9e322018-08-17 14:39:04 +00001163 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
Sam McCalld0d43e62017-12-04 12:51:49 +00001164 auto KZF = [NSW](const APInt &KnownZero, unsigned ShiftAmt) {
1165 APInt KZResult = KnownZero << ShiftAmt;
1166 KZResult.setLowBits(ShiftAmt); // Low bits known 0.
Evgeny Stupachenkod7f9c352016-08-24 23:01:33 +00001167 // If this shift has "nsw" keyword, then the result is either a poison
1168 // value or has the same sign bit as the first operand.
Sam McCalld0d43e62017-12-04 12:51:49 +00001169 if (NSW && KnownZero.isSignBitSet())
1170 KZResult.setSignBit();
1171 return KZResult;
Hal Finkelf2199b22015-10-23 20:37:08 +00001172 };
1173
Sam McCalld0d43e62017-12-04 12:51:49 +00001174 auto KOF = [NSW](const APInt &KnownOne, unsigned ShiftAmt) {
1175 APInt KOResult = KnownOne << ShiftAmt;
1176 if (NSW && KnownOne.isSignBitSet())
1177 KOResult.setSignBit();
1178 return KOResult;
1179 };
1180
1181 computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
Chris Lattner965c7692008-06-02 01:18:21 +00001182 break;
Hal Finkelf2199b22015-10-23 20:37:08 +00001183 }
1184 case Instruction::LShr: {
Sanjay Patelb7d12382017-10-16 14:46:37 +00001185 // (lshr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Sam McCalld0d43e62017-12-04 12:51:49 +00001186 auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
1187 APInt KZResult = KnownZero.lshr(ShiftAmt);
1188 // High bits known zero.
1189 KZResult.setHighBits(ShiftAmt);
1190 return KZResult;
Hal Finkelf2199b22015-10-23 20:37:08 +00001191 };
Craig Topper1bef2c82012-12-22 19:15:35 +00001192
Sam McCalld0d43e62017-12-04 12:51:49 +00001193 auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
1194 return KnownOne.lshr(ShiftAmt);
1195 };
1196
1197 computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
Chris Lattner965c7692008-06-02 01:18:21 +00001198 break;
Hal Finkelf2199b22015-10-23 20:37:08 +00001199 }
1200 case Instruction::AShr: {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001201 // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Sam McCalld0d43e62017-12-04 12:51:49 +00001202 auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
1203 return KnownZero.ashr(ShiftAmt);
Hal Finkelf2199b22015-10-23 20:37:08 +00001204 };
Craig Topper1bef2c82012-12-22 19:15:35 +00001205
Sam McCalld0d43e62017-12-04 12:51:49 +00001206 auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
1207 return KnownOne.ashr(ShiftAmt);
1208 };
1209
1210 computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
Chris Lattner965c7692008-06-02 01:18:21 +00001211 break;
Hal Finkelf2199b22015-10-23 20:37:08 +00001212 }
Chris Lattner965c7692008-06-02 01:18:21 +00001213 case Instruction::Sub: {
Florian Hahn19f9e322018-08-17 14:39:04 +00001214 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
Jay Foada0653a32014-05-14 21:14:37 +00001215 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
Craig Topperb45eabc2017-04-26 16:39:58 +00001216 Known, Known2, Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001217 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001218 }
Chris Lattner965c7692008-06-02 01:18:21 +00001219 case Instruction::Add: {
Florian Hahn19f9e322018-08-17 14:39:04 +00001220 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
Jay Foada0653a32014-05-14 21:14:37 +00001221 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
Craig Topperb45eabc2017-04-26 16:39:58 +00001222 Known, Known2, Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001223 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001224 }
1225 case Instruction::SRem:
1226 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001227 APInt RA = Rem->getValue().abs();
1228 if (RA.isPowerOf2()) {
1229 APInt LowBits = RA - 1;
Craig Topperb45eabc2017-04-26 16:39:58 +00001230 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001231
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001232 // The low bits of the first operand are unchanged by the srem.
Craig Topperb45eabc2017-04-26 16:39:58 +00001233 Known.Zero = Known2.Zero & LowBits;
1234 Known.One = Known2.One & LowBits;
Chris Lattner965c7692008-06-02 01:18:21 +00001235
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001236 // If the first operand is non-negative or has all low bits zero, then
1237 // the upper bits are all zero.
Craig Topperca48af32017-04-29 16:43:11 +00001238 if (Known2.isNonNegative() || LowBits.isSubsetOf(Known2.Zero))
Craig Topperb45eabc2017-04-26 16:39:58 +00001239 Known.Zero |= ~LowBits;
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001240
1241 // If the first operand is negative and not all low bits are zero, then
1242 // the upper bits are all one.
Craig Topperca48af32017-04-29 16:43:11 +00001243 if (Known2.isNegative() && LowBits.intersects(Known2.One))
Craig Topperb45eabc2017-04-26 16:39:58 +00001244 Known.One |= ~LowBits;
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001245
Craig Topperb45eabc2017-04-26 16:39:58 +00001246 assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
Craig Topperda886c62017-04-16 21:46:12 +00001247 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001248 }
1249 }
Nick Lewyckye4679792011-03-07 01:50:10 +00001250
1251 // The sign bit is the LHS's sign bit, except when the result of the
1252 // remainder is zero.
Craig Topperb45eabc2017-04-26 16:39:58 +00001253 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
Craig Topperda886c62017-04-16 21:46:12 +00001254 // If it's known zero, our sign bit is also zero.
Craig Topperca48af32017-04-29 16:43:11 +00001255 if (Known2.isNonNegative())
1256 Known.makeNonNegative();
Nick Lewyckye4679792011-03-07 01:50:10 +00001257
Chris Lattner965c7692008-06-02 01:18:21 +00001258 break;
1259 case Instruction::URem: {
1260 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Benjamin Kramer46e38f32016-06-08 10:01:20 +00001261 const APInt &RA = Rem->getValue();
Chris Lattner965c7692008-06-02 01:18:21 +00001262 if (RA.isPowerOf2()) {
1263 APInt LowBits = (RA - 1);
Craig Topperb45eabc2017-04-26 16:39:58 +00001264 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1265 Known.Zero |= ~LowBits;
1266 Known.One &= LowBits;
Chris Lattner965c7692008-06-02 01:18:21 +00001267 break;
1268 }
1269 }
1270
1271 // Since the result is less than or equal to either operand, any leading
1272 // zero bits in either operand must also exist in the result.
Craig Topperb45eabc2017-04-26 16:39:58 +00001273 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1274 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001275
Craig Topper8df66c62017-05-12 17:20:30 +00001276 unsigned Leaders =
1277 std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
Craig Topperf0aeee02017-05-05 17:36:09 +00001278 Known.resetAll();
Craig Topperb45eabc2017-04-26 16:39:58 +00001279 Known.Zero.setHighBits(Leaders);
Chris Lattner965c7692008-06-02 01:18:21 +00001280 break;
1281 }
1282
Victor Hernandeza3aaf852009-10-17 01:18:07 +00001283 case Instruction::Alloca: {
Pete Cooper35b00d52016-08-13 01:05:32 +00001284 const AllocaInst *AI = cast<AllocaInst>(I);
Chris Lattner965c7692008-06-02 01:18:21 +00001285 unsigned Align = AI->getAlignment();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001286 if (Align == 0)
Eduard Burtescu90c44492016-01-18 00:10:01 +00001287 Align = Q.DL.getABITypeAlignment(AI->getAllocatedType());
Craig Topper1bef2c82012-12-22 19:15:35 +00001288
Chris Lattner965c7692008-06-02 01:18:21 +00001289 if (Align > 0)
Craig Topperb45eabc2017-04-26 16:39:58 +00001290 Known.Zero.setLowBits(countTrailingZeros(Align));
Chris Lattner965c7692008-06-02 01:18:21 +00001291 break;
1292 }
1293 case Instruction::GetElementPtr: {
1294 // Analyze all of the subscripts of this getelementptr instruction
1295 // to determine if we can prove known low zero bits.
Craig Topperb45eabc2017-04-26 16:39:58 +00001296 KnownBits LocalKnown(BitWidth);
1297 computeKnownBits(I->getOperand(0), LocalKnown, Depth + 1, Q);
Craig Topper8df66c62017-05-12 17:20:30 +00001298 unsigned TrailZ = LocalKnown.countMinTrailingZeros();
Chris Lattner965c7692008-06-02 01:18:21 +00001299
1300 gep_type_iterator GTI = gep_type_begin(I);
1301 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1302 Value *Index = I->getOperand(i);
Peter Collingbourneab85225b2016-12-02 02:24:42 +00001303 if (StructType *STy = GTI.getStructTypeOrNull()) {
Chris Lattner965c7692008-06-02 01:18:21 +00001304 // Handle struct member offset arithmetic.
Matt Arsenault74742a12013-08-19 21:43:16 +00001305
1306 // Handle case when index is vector zeroinitializer
1307 Constant *CIndex = cast<Constant>(Index);
1308 if (CIndex->isZeroValue())
1309 continue;
1310
1311 if (CIndex->getType()->isVectorTy())
1312 Index = CIndex->getSplatValue();
1313
Chris Lattner965c7692008-06-02 01:18:21 +00001314 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001315 const StructLayout *SL = Q.DL.getStructLayout(STy);
Chris Lattner965c7692008-06-02 01:18:21 +00001316 uint64_t Offset = SL->getElementOffset(Idx);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001317 TrailZ = std::min<unsigned>(TrailZ,
1318 countTrailingZeros(Offset));
Chris Lattner965c7692008-06-02 01:18:21 +00001319 } else {
1320 // Handle array index arithmetic.
Chris Lattner229907c2011-07-18 04:54:35 +00001321 Type *IndexedTy = GTI.getIndexedType();
Jay Foad5a29c362014-05-15 12:12:55 +00001322 if (!IndexedTy->isSized()) {
1323 TrailZ = 0;
1324 break;
1325 }
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001326 unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001327 uint64_t TypeSize = Q.DL.getTypeAllocSize(IndexedTy);
Craig Topperb45eabc2017-04-26 16:39:58 +00001328 LocalKnown.Zero = LocalKnown.One = APInt(GEPOpiBits, 0);
1329 computeKnownBits(Index, LocalKnown, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001330 TrailZ = std::min(TrailZ,
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001331 unsigned(countTrailingZeros(TypeSize) +
Craig Topper8df66c62017-05-12 17:20:30 +00001332 LocalKnown.countMinTrailingZeros()));
Chris Lattner965c7692008-06-02 01:18:21 +00001333 }
1334 }
Craig Topper1bef2c82012-12-22 19:15:35 +00001335
Craig Topperb45eabc2017-04-26 16:39:58 +00001336 Known.Zero.setLowBits(TrailZ);
Chris Lattner965c7692008-06-02 01:18:21 +00001337 break;
1338 }
1339 case Instruction::PHI: {
Pete Cooper35b00d52016-08-13 01:05:32 +00001340 const PHINode *P = cast<PHINode>(I);
Chris Lattner965c7692008-06-02 01:18:21 +00001341 // Handle the case of a simple two-predecessor recurrence PHI.
1342 // There's a lot more that could theoretically be done here, but
1343 // this is sufficient to catch some interesting cases.
1344 if (P->getNumIncomingValues() == 2) {
1345 for (unsigned i = 0; i != 2; ++i) {
1346 Value *L = P->getIncomingValue(i);
1347 Value *R = P->getIncomingValue(!i);
Dan Gohman80ca01c2009-07-17 20:47:02 +00001348 Operator *LU = dyn_cast<Operator>(L);
Chris Lattner965c7692008-06-02 01:18:21 +00001349 if (!LU)
1350 continue;
Dan Gohman80ca01c2009-07-17 20:47:02 +00001351 unsigned Opcode = LU->getOpcode();
Chris Lattner965c7692008-06-02 01:18:21 +00001352 // Check for operations that have the property that if
1353 // both their operands have low zero bits, the result
Artur Pilipenkobc76eca2016-08-22 13:14:07 +00001354 // will have low zero bits.
Chris Lattner965c7692008-06-02 01:18:21 +00001355 if (Opcode == Instruction::Add ||
1356 Opcode == Instruction::Sub ||
1357 Opcode == Instruction::And ||
1358 Opcode == Instruction::Or ||
1359 Opcode == Instruction::Mul) {
1360 Value *LL = LU->getOperand(0);
1361 Value *LR = LU->getOperand(1);
1362 // Find a recurrence.
1363 if (LL == I)
1364 L = LR;
1365 else if (LR == I)
1366 L = LL;
1367 else
1368 break;
1369 // Ok, we have a PHI of the form L op= R. Check for low
1370 // zero bits.
Craig Topperb45eabc2017-04-26 16:39:58 +00001371 computeKnownBits(R, Known2, Depth + 1, Q);
David Greeneaebd9e02008-10-27 23:24:03 +00001372
1373 // We need to take the minimum number of known bits
Craig Topperb45eabc2017-04-26 16:39:58 +00001374 KnownBits Known3(Known);
1375 computeKnownBits(L, Known3, Depth + 1, Q);
David Greeneaebd9e02008-10-27 23:24:03 +00001376
Craig Topper8df66c62017-05-12 17:20:30 +00001377 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1378 Known3.countMinTrailingZeros()));
Artur Pilipenkoc6eb6bd2016-10-12 16:18:43 +00001379
Artur Pilipenkoc6eb6bd2016-10-12 16:18:43 +00001380 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(LU);
Florian Hahn19f9e322018-08-17 14:39:04 +00001381 if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
Artur Pilipenkoc6eb6bd2016-10-12 16:18:43 +00001382 // If initial value of recurrence is nonnegative, and we are adding
1383 // a nonnegative number with nsw, the result can only be nonnegative
1384 // or poison value regardless of the number of times we execute the
1385 // add in phi recurrence. If initial value is negative and we are
1386 // adding a negative number with nsw, the result can only be
1387 // negative or poison value. Similar arguments apply to sub and mul.
1388 //
1389 // (add non-negative, non-negative) --> non-negative
1390 // (add negative, negative) --> negative
1391 if (Opcode == Instruction::Add) {
Craig Topperca48af32017-04-29 16:43:11 +00001392 if (Known2.isNonNegative() && Known3.isNonNegative())
1393 Known.makeNonNegative();
1394 else if (Known2.isNegative() && Known3.isNegative())
1395 Known.makeNegative();
Artur Pilipenkoc6eb6bd2016-10-12 16:18:43 +00001396 }
1397
1398 // (sub nsw non-negative, negative) --> non-negative
1399 // (sub nsw negative, non-negative) --> negative
1400 else if (Opcode == Instruction::Sub && LL == I) {
Craig Topperca48af32017-04-29 16:43:11 +00001401 if (Known2.isNonNegative() && Known3.isNegative())
1402 Known.makeNonNegative();
1403 else if (Known2.isNegative() && Known3.isNonNegative())
1404 Known.makeNegative();
Artur Pilipenkoc6eb6bd2016-10-12 16:18:43 +00001405 }
1406
1407 // (mul nsw non-negative, non-negative) --> non-negative
Craig Topperca48af32017-04-29 16:43:11 +00001408 else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
1409 Known3.isNonNegative())
1410 Known.makeNonNegative();
Artur Pilipenkoc6eb6bd2016-10-12 16:18:43 +00001411 }
1412
Chris Lattner965c7692008-06-02 01:18:21 +00001413 break;
1414 }
1415 }
1416 }
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001417
Nick Lewyckyac0b62c2011-02-10 23:54:10 +00001418 // Unreachable blocks may have zero-operand PHI nodes.
1419 if (P->getNumIncomingValues() == 0)
Jay Foad5a29c362014-05-15 12:12:55 +00001420 break;
Nick Lewyckyac0b62c2011-02-10 23:54:10 +00001421
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001422 // Otherwise take the unions of the known bit sets of the operands,
1423 // taking conservative care to avoid excessive recursion.
Craig Topperb45eabc2017-04-26 16:39:58 +00001424 if (Depth < MaxDepth - 1 && !Known.Zero && !Known.One) {
Duncan Sands7dc3d472011-03-08 12:39:03 +00001425 // Skip if every incoming value references to ourself.
Nuno Lopes0d44a502012-07-03 21:15:40 +00001426 if (dyn_cast_or_null<UndefValue>(P->hasConstantValue()))
Duncan Sands7dc3d472011-03-08 12:39:03 +00001427 break;
1428
Craig Topperb45eabc2017-04-26 16:39:58 +00001429 Known.Zero.setAllBits();
1430 Known.One.setAllBits();
Pete Cooper833f34d2015-05-12 20:05:31 +00001431 for (Value *IncValue : P->incoming_values()) {
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001432 // Skip direct self references.
Pete Cooper833f34d2015-05-12 20:05:31 +00001433 if (IncValue == P) continue;
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001434
Craig Topperb45eabc2017-04-26 16:39:58 +00001435 Known2 = KnownBits(BitWidth);
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001436 // Recurse, but cap the recursion to one level, because we don't
1437 // want to waste time spinning around in loops.
Craig Topperb45eabc2017-04-26 16:39:58 +00001438 computeKnownBits(IncValue, Known2, MaxDepth - 1, Q);
1439 Known.Zero &= Known2.Zero;
1440 Known.One &= Known2.One;
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001441 // If all bits have been ruled out, there's no need to check
1442 // more operands.
Craig Topperb45eabc2017-04-26 16:39:58 +00001443 if (!Known.Zero && !Known.One)
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001444 break;
1445 }
1446 }
Chris Lattner965c7692008-06-02 01:18:21 +00001447 break;
1448 }
1449 case Instruction::Call:
Jingyue Wu37fcb592014-06-19 16:50:16 +00001450 case Instruction::Invoke:
Hal Finkel6fd5e1f2016-07-11 02:25:14 +00001451 // If range metadata is attached to this call, set known bits from that,
1452 // and then intersect with known bits based on other properties of the
1453 // function.
Florian Hahn19f9e322018-08-17 14:39:04 +00001454 if (MDNode *MD =
1455 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
Craig Topperf42b23f2017-04-28 06:28:56 +00001456 computeKnownBitsFromRangeMetadata(*MD, Known);
Pete Cooper35b00d52016-08-13 01:05:32 +00001457 if (const Value *RV = ImmutableCallSite(I).getReturnedArgOperand()) {
Craig Topperb45eabc2017-04-26 16:39:58 +00001458 computeKnownBits(RV, Known2, Depth + 1, Q);
1459 Known.Zero |= Known2.Zero;
1460 Known.One |= Known2.One;
Hal Finkel6fd5e1f2016-07-11 02:25:14 +00001461 }
Pete Cooper35b00d52016-08-13 01:05:32 +00001462 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
Chris Lattner965c7692008-06-02 01:18:21 +00001463 switch (II->getIntrinsicID()) {
1464 default: break;
Chad Rosier85204292017-01-17 17:23:51 +00001465 case Intrinsic::bitreverse:
Craig Topperb45eabc2017-04-26 16:39:58 +00001466 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1467 Known.Zero |= Known2.Zero.reverseBits();
1468 Known.One |= Known2.One.reverseBits();
Chad Rosier85204292017-01-17 17:23:51 +00001469 break;
Philip Reames675418e2015-10-06 20:20:45 +00001470 case Intrinsic::bswap:
Craig Topperb45eabc2017-04-26 16:39:58 +00001471 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1472 Known.Zero |= Known2.Zero.byteSwap();
1473 Known.One |= Known2.One.byteSwap();
Philip Reames675418e2015-10-06 20:20:45 +00001474 break;
Craig Topper868813f2017-05-08 17:22:34 +00001475 case Intrinsic::ctlz: {
1476 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1477 // If we have a known 1, its position is our upper bound.
1478 unsigned PossibleLZ = Known2.One.countLeadingZeros();
Benjamin Kramer4ee57472011-12-24 17:31:46 +00001479 // If this call is undefined for 0, the result will be less than 2^n.
1480 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
Craig Topper868813f2017-05-08 17:22:34 +00001481 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1482 unsigned LowBits = Log2_32(PossibleLZ)+1;
1483 Known.Zero.setBitsFrom(LowBits);
1484 break;
1485 }
1486 case Intrinsic::cttz: {
1487 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1488 // If we have a known 1, its position is our upper bound.
1489 unsigned PossibleTZ = Known2.One.countTrailingZeros();
1490 // If this call is undefined for 0, the result will be less than 2^n.
1491 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1492 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1493 unsigned LowBits = Log2_32(PossibleTZ)+1;
Craig Topperb45eabc2017-04-26 16:39:58 +00001494 Known.Zero.setBitsFrom(LowBits);
Benjamin Kramer4ee57472011-12-24 17:31:46 +00001495 break;
1496 }
1497 case Intrinsic::ctpop: {
Craig Topperb45eabc2017-04-26 16:39:58 +00001498 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
Philip Reamesddcf6b32015-10-14 22:42:12 +00001499 // We can bound the space the count needs. Also, bits known to be zero
1500 // can't contribute to the population.
Craig Topper8df66c62017-05-12 17:20:30 +00001501 unsigned BitsPossiblySet = Known2.countMaxPopulation();
Craig Topper66df10f2017-04-14 06:43:34 +00001502 unsigned LowBits = Log2_32(BitsPossiblySet)+1;
Craig Topperb45eabc2017-04-26 16:39:58 +00001503 Known.Zero.setBitsFrom(LowBits);
Philip Reamesddcf6b32015-10-14 22:42:12 +00001504 // TODO: we could bound KnownOne using the lower bound on the number
1505 // of bits which might be set provided by popcnt KnownOne2.
Chris Lattner965c7692008-06-02 01:18:21 +00001506 break;
1507 }
Nikita Popov687b92c2018-12-02 14:14:11 +00001508 case Intrinsic::fshr:
1509 case Intrinsic::fshl: {
1510 const APInt *SA;
1511 if (!match(I->getOperand(2), m_APInt(SA)))
1512 break;
1513
1514 // Normalize to funnel shift left.
1515 uint64_t ShiftAmt = SA->urem(BitWidth);
1516 if (II->getIntrinsicID() == Intrinsic::fshr)
1517 ShiftAmt = BitWidth - ShiftAmt;
1518
1519 KnownBits Known3(Known);
1520 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1521 computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q);
1522
1523 Known.Zero =
1524 Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
1525 Known.One =
1526 Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
1527 break;
1528 }
Chad Rosierb3628842011-05-26 23:13:19 +00001529 case Intrinsic::x86_sse42_crc32_64_64:
Craig Topperb45eabc2017-04-26 16:39:58 +00001530 Known.Zero.setBitsFrom(32);
Evan Cheng2a746bf2011-05-22 18:25:30 +00001531 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001532 }
1533 }
1534 break;
Bjorn Pettersson39616032016-10-06 09:56:21 +00001535 case Instruction::ExtractElement:
1536 // Look through extract element. At the moment we keep this simple and skip
1537 // tracking the specific element. But at least we might find information
1538 // valid for all elements of the vector (for example if vector is sign
1539 // extended, shifted, etc).
Craig Topperb45eabc2017-04-26 16:39:58 +00001540 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
Bjorn Pettersson39616032016-10-06 09:56:21 +00001541 break;
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001542 case Instruction::ExtractValue:
1543 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
Pete Cooper35b00d52016-08-13 01:05:32 +00001544 const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001545 if (EVI->getNumIndices() != 1) break;
1546 if (EVI->getIndices()[0] == 0) {
1547 switch (II->getIntrinsicID()) {
1548 default: break;
1549 case Intrinsic::uadd_with_overflow:
1550 case Intrinsic::sadd_with_overflow:
Jay Foada0653a32014-05-14 21:14:37 +00001551 computeKnownBitsAddSub(true, II->getArgOperand(0),
Craig Topperb45eabc2017-04-26 16:39:58 +00001552 II->getArgOperand(1), false, Known, Known2,
1553 Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001554 break;
1555 case Intrinsic::usub_with_overflow:
1556 case Intrinsic::ssub_with_overflow:
Jay Foada0653a32014-05-14 21:14:37 +00001557 computeKnownBitsAddSub(false, II->getArgOperand(0),
Craig Topperb45eabc2017-04-26 16:39:58 +00001558 II->getArgOperand(1), false, Known, Known2,
1559 Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001560 break;
Nick Lewyckyfa306072012-03-18 23:28:48 +00001561 case Intrinsic::umul_with_overflow:
1562 case Intrinsic::smul_with_overflow:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001563 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
Craig Topperb45eabc2017-04-26 16:39:58 +00001564 Known, Known2, Depth, Q);
Nick Lewyckyfa306072012-03-18 23:28:48 +00001565 break;
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001566 }
1567 }
1568 }
Chris Lattner965c7692008-06-02 01:18:21 +00001569 }
Jingyue Wu12b0c282015-06-15 05:46:29 +00001570}
1571
1572/// Determine which bits of V are known to be either zero or one and return
Craig Topper6e11a052017-05-08 16:22:48 +00001573/// them.
1574KnownBits computeKnownBits(const Value *V, unsigned Depth, const Query &Q) {
1575 KnownBits Known(getBitWidth(V->getType(), Q.DL));
1576 computeKnownBits(V, Known, Depth, Q);
1577 return Known;
1578}
1579
1580/// Determine which bits of V are known to be either zero or one and return
Craig Topperb45eabc2017-04-26 16:39:58 +00001581/// them in the Known bit set.
Jingyue Wu12b0c282015-06-15 05:46:29 +00001582///
1583/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
1584/// we cannot optimize based on the assumption that it is zero without changing
1585/// it to be an explicit zero. If we don't change it to zero, other code could
1586/// optimized based on the contradictory assumption that it is non-zero.
1587/// Because instcombine aggressively folds operations with undef args anyway,
1588/// this won't lose us code quality.
1589///
1590/// This function is defined on values with integer type, values with pointer
1591/// type, and vectors of integers. In the case
1592/// where V is a vector, known zero, and known one values are the
1593/// same width as the vector element, and the bit is set only if it is true
1594/// for all of the elements in the vector.
Craig Topperb45eabc2017-04-26 16:39:58 +00001595void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
1596 const Query &Q) {
Jingyue Wu12b0c282015-06-15 05:46:29 +00001597 assert(V && "No Value?");
1598 assert(Depth <= MaxDepth && "Limit Search Depth");
Craig Topperb45eabc2017-04-26 16:39:58 +00001599 unsigned BitWidth = Known.getBitWidth();
Jingyue Wu12b0c282015-06-15 05:46:29 +00001600
Craig Topperfde47232017-07-09 07:04:03 +00001601 assert((V->getType()->isIntOrIntVectorTy(BitWidth) ||
Craig Topper95d23472017-07-09 07:04:00 +00001602 V->getType()->isPtrOrPtrVectorTy()) &&
Sanjay Pateldba8b4c2016-06-02 20:01:37 +00001603 "Not integer or pointer type!");
Elena Demikhovsky945b7e52018-02-14 06:58:08 +00001604
1605 Type *ScalarTy = V->getType()->getScalarType();
1606 unsigned ExpectedWidth = ScalarTy->isPointerTy() ?
1607 Q.DL.getIndexTypeSizeInBits(ScalarTy) : Q.DL.getTypeSizeInBits(ScalarTy);
1608 assert(ExpectedWidth == BitWidth && "V and Known should have same BitWidth");
Craig Topperd73c6b42017-03-23 07:06:39 +00001609 (void)BitWidth;
Elena Demikhovsky945b7e52018-02-14 06:58:08 +00001610 (void)ExpectedWidth;
Jingyue Wu12b0c282015-06-15 05:46:29 +00001611
Sanjay Patelc96f6db2016-09-16 21:20:36 +00001612 const APInt *C;
1613 if (match(V, m_APInt(C))) {
1614 // We know all of the bits for a scalar constant or a splat vector constant!
Craig Topperb45eabc2017-04-26 16:39:58 +00001615 Known.One = *C;
1616 Known.Zero = ~Known.One;
Jingyue Wu12b0c282015-06-15 05:46:29 +00001617 return;
1618 }
1619 // Null and aggregate-zero are all-zeros.
Sanjay Patele8dc0902016-05-23 17:57:54 +00001620 if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
Craig Topperf0aeee02017-05-05 17:36:09 +00001621 Known.setAllZero();
Jingyue Wu12b0c282015-06-15 05:46:29 +00001622 return;
1623 }
1624 // Handle a constant vector by taking the intersection of the known bits of
David Majnemer3918cdd2016-05-04 06:13:33 +00001625 // each element.
Pete Cooper35b00d52016-08-13 01:05:32 +00001626 if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) {
Jingyue Wu12b0c282015-06-15 05:46:29 +00001627 // We know that CDS must be a vector of integers. Take the intersection of
1628 // each element.
Craig Topperb45eabc2017-04-26 16:39:58 +00001629 Known.Zero.setAllBits(); Known.One.setAllBits();
Jingyue Wu12b0c282015-06-15 05:46:29 +00001630 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
Craig Topperb98ee582017-10-21 16:35:39 +00001631 APInt Elt = CDS->getElementAsAPInt(i);
Craig Topperb45eabc2017-04-26 16:39:58 +00001632 Known.Zero &= ~Elt;
1633 Known.One &= Elt;
Jingyue Wu12b0c282015-06-15 05:46:29 +00001634 }
1635 return;
1636 }
1637
Pete Cooper35b00d52016-08-13 01:05:32 +00001638 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
David Majnemer3918cdd2016-05-04 06:13:33 +00001639 // We know that CV must be a vector of integers. Take the intersection of
1640 // each element.
Craig Topperb45eabc2017-04-26 16:39:58 +00001641 Known.Zero.setAllBits(); Known.One.setAllBits();
David Majnemer3918cdd2016-05-04 06:13:33 +00001642 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1643 Constant *Element = CV->getAggregateElement(i);
1644 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
1645 if (!ElementCI) {
Craig Topperf0aeee02017-05-05 17:36:09 +00001646 Known.resetAll();
David Majnemer3918cdd2016-05-04 06:13:33 +00001647 return;
1648 }
Craig Topperb98ee582017-10-21 16:35:39 +00001649 const APInt &Elt = ElementCI->getValue();
Craig Topperb45eabc2017-04-26 16:39:58 +00001650 Known.Zero &= ~Elt;
1651 Known.One &= Elt;
David Majnemer3918cdd2016-05-04 06:13:33 +00001652 }
1653 return;
1654 }
1655
Jingyue Wu12b0c282015-06-15 05:46:29 +00001656 // Start out not knowing anything.
Craig Topperf0aeee02017-05-05 17:36:09 +00001657 Known.resetAll();
Jingyue Wu12b0c282015-06-15 05:46:29 +00001658
Duncan P. N. Exon Smithb1b208a2016-09-24 20:42:02 +00001659 // We can't imply anything about undefs.
1660 if (isa<UndefValue>(V))
1661 return;
1662
1663 // There's no point in looking through other users of ConstantData for
1664 // assumptions. Confirm that we've handled them all.
1665 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
1666
Jingyue Wu12b0c282015-06-15 05:46:29 +00001667 // Limit search depth.
1668 // All recursive calls that increase depth must come after this.
1669 if (Depth == MaxDepth)
1670 return;
1671
1672 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
1673 // the bits of its aliasee.
Pete Cooper35b00d52016-08-13 01:05:32 +00001674 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
Sanjoy Das5ce32722016-04-08 00:48:30 +00001675 if (!GA->isInterposable())
Craig Topperb45eabc2017-04-26 16:39:58 +00001676 computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
Jingyue Wu12b0c282015-06-15 05:46:29 +00001677 return;
1678 }
1679
Pete Cooper35b00d52016-08-13 01:05:32 +00001680 if (const Operator *I = dyn_cast<Operator>(V))
Craig Topperb45eabc2017-04-26 16:39:58 +00001681 computeKnownBitsFromOperator(I, Known, Depth, Q);
Sanjay Patela67559c2015-09-25 20:12:43 +00001682
Craig Topperb45eabc2017-04-26 16:39:58 +00001683 // Aligned pointers have trailing zeros - refine Known.Zero set
Artur Pilipenko029d8532015-09-30 11:55:45 +00001684 if (V->getType()->isPointerTy()) {
Artur Pilipenkoae51afc2016-02-24 12:25:10 +00001685 unsigned Align = V->getPointerAlignment(Q.DL);
Artur Pilipenko029d8532015-09-30 11:55:45 +00001686 if (Align)
Craig Topperb45eabc2017-04-26 16:39:58 +00001687 Known.Zero.setLowBits(countTrailingZeros(Align));
Artur Pilipenko029d8532015-09-30 11:55:45 +00001688 }
1689
Craig Topperb45eabc2017-04-26 16:39:58 +00001690 // computeKnownBitsFromAssume strictly refines Known.
1691 // Therefore, we run them after computeKnownBitsFromOperator.
Jingyue Wu12b0c282015-06-15 05:46:29 +00001692
1693 // Check whether a nearby assume intrinsic can determine some known bits.
Craig Topperb45eabc2017-04-26 16:39:58 +00001694 computeKnownBitsFromAssume(V, Known, Depth, Q);
Jingyue Wu12b0c282015-06-15 05:46:29 +00001695
Craig Topperb45eabc2017-04-26 16:39:58 +00001696 assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
Chris Lattner965c7692008-06-02 01:18:21 +00001697}
1698
Sanjay Patelaee84212014-11-04 16:27:42 +00001699/// Return true if the given value is known to have exactly one
Duncan Sandsd3951082011-01-25 09:38:29 +00001700/// bit set when defined. For vectors return true if every element is known to
Sanjay Patelaee84212014-11-04 16:27:42 +00001701/// be a power of two when defined. Supports values with integer or pointer
Duncan Sandsd3951082011-01-25 09:38:29 +00001702/// types and vectors of integers.
Pete Cooper35b00d52016-08-13 01:05:32 +00001703bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001704 const Query &Q) {
Craig Topper7227eba2017-08-21 22:56:12 +00001705 assert(Depth <= MaxDepth && "Limit Search Depth");
1706
Simon Pilgrim9f2ae7e2018-02-06 18:39:23 +00001707 // Attempt to match against constants.
1708 if (OrZero && match(V, m_Power2OrZero()))
1709 return true;
1710 if (match(V, m_Power2()))
1711 return true;
Duncan Sandsd3951082011-01-25 09:38:29 +00001712
1713 // 1 << X is clearly a power of two if the one is not shifted off the end. If
1714 // it is shifted off the end then the result is undefined.
1715 if (match(V, m_Shl(m_One(), m_Value())))
1716 return true;
1717
Craig Topperbcfd2d12017-04-20 16:56:25 +00001718 // (signmask) >>l X is clearly a power of two if the one is not shifted off
1719 // the bottom. If it is shifted off the bottom then the result is undefined.
1720 if (match(V, m_LShr(m_SignMask(), m_Value())))
Duncan Sandsd3951082011-01-25 09:38:29 +00001721 return true;
1722
1723 // The remaining tests are all recursive, so bail out if we hit the limit.
1724 if (Depth++ == MaxDepth)
1725 return false;
1726
Craig Topper9f008862014-04-15 04:59:12 +00001727 Value *X = nullptr, *Y = nullptr;
Sanjay Patel41160c22015-12-30 22:40:52 +00001728 // A shift left or a logical shift right of a power of two is a power of two
1729 // or zero.
Duncan Sands985ba632011-10-28 18:30:05 +00001730 if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
Sanjay Patel41160c22015-12-30 22:40:52 +00001731 match(V, m_LShr(m_Value(X), m_Value()))))
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001732 return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q);
Duncan Sands985ba632011-10-28 18:30:05 +00001733
Pete Cooper35b00d52016-08-13 01:05:32 +00001734 if (const ZExtInst *ZI = dyn_cast<ZExtInst>(V))
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001735 return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001736
Pete Cooper35b00d52016-08-13 01:05:32 +00001737 if (const SelectInst *SI = dyn_cast<SelectInst>(V))
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001738 return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) &&
1739 isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q);
Duncan Sandsba286d72011-10-26 20:55:21 +00001740
Duncan Sandsba286d72011-10-26 20:55:21 +00001741 if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
1742 // A power of two and'd with anything is a power of two or zero.
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001743 if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) ||
1744 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q))
Duncan Sandsba286d72011-10-26 20:55:21 +00001745 return true;
1746 // X & (-X) is always a power of two or zero.
1747 if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
1748 return true;
1749 return false;
1750 }
Duncan Sandsd3951082011-01-25 09:38:29 +00001751
David Majnemerb7d54092013-07-30 21:01:36 +00001752 // Adding a power-of-two or zero to the same power-of-two or zero yields
1753 // either the original power-of-two, a larger power-of-two or zero.
1754 if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
Pete Cooper35b00d52016-08-13 01:05:32 +00001755 const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
Florian Hahn19f9e322018-08-17 14:39:04 +00001756 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
1757 Q.IIQ.hasNoSignedWrap(VOBO)) {
David Majnemerb7d54092013-07-30 21:01:36 +00001758 if (match(X, m_And(m_Specific(Y), m_Value())) ||
1759 match(X, m_And(m_Value(), m_Specific(Y))))
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001760 if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q))
David Majnemerb7d54092013-07-30 21:01:36 +00001761 return true;
1762 if (match(Y, m_And(m_Specific(X), m_Value())) ||
1763 match(Y, m_And(m_Value(), m_Specific(X))))
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001764 if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q))
David Majnemerb7d54092013-07-30 21:01:36 +00001765 return true;
1766
1767 unsigned BitWidth = V->getType()->getScalarSizeInBits();
Craig Topperb45eabc2017-04-26 16:39:58 +00001768 KnownBits LHSBits(BitWidth);
1769 computeKnownBits(X, LHSBits, Depth, Q);
David Majnemerb7d54092013-07-30 21:01:36 +00001770
Craig Topperb45eabc2017-04-26 16:39:58 +00001771 KnownBits RHSBits(BitWidth);
1772 computeKnownBits(Y, RHSBits, Depth, Q);
David Majnemerb7d54092013-07-30 21:01:36 +00001773 // If i8 V is a power of two or zero:
1774 // ZeroBits: 1 1 1 0 1 1 1 1
1775 // ~ZeroBits: 0 0 0 1 0 0 0 0
Craig Topperb45eabc2017-04-26 16:39:58 +00001776 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
David Majnemerb7d54092013-07-30 21:01:36 +00001777 // If OrZero isn't set, we cannot give back a zero result.
1778 // Make sure either the LHS or RHS has a bit set.
Craig Topperb45eabc2017-04-26 16:39:58 +00001779 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
David Majnemerb7d54092013-07-30 21:01:36 +00001780 return true;
1781 }
1782 }
David Majnemerbeab5672013-05-18 19:30:37 +00001783
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001784 // An exact divide or right shift can only shift off zero bits, so the result
Nick Lewyckyf0469af2011-03-21 21:40:32 +00001785 // is a power of two only if the first operand is a power of two and not
1786 // copying a sign bit (sdiv int_min, 2).
Benjamin Kramer9442cd02012-01-01 17:55:30 +00001787 if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
1788 match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
Hal Finkel60db0582014-09-07 18:57:58 +00001789 return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero,
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001790 Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001791 }
1792
Duncan Sandsd3951082011-01-25 09:38:29 +00001793 return false;
1794}
1795
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001796/// Test whether a GEP's result is known to be non-null.
Chandler Carruth80d3e562012-12-07 02:08:58 +00001797///
1798/// Uses properties inherent in a GEP to try to determine whether it is known
1799/// to be non-null.
1800///
1801/// Currently this routine does not support vector GEPs.
Pete Cooper35b00d52016-08-13 01:05:32 +00001802static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001803 const Query &Q) {
Manoj Gupta77eeac32018-07-09 22:27:23 +00001804 const Function *F = nullptr;
1805 if (const Instruction *I = dyn_cast<Instruction>(GEP))
1806 F = I->getFunction();
1807
1808 if (!GEP->isInBounds() ||
1809 NullPointerIsDefined(F, GEP->getPointerAddressSpace()))
Chandler Carruth80d3e562012-12-07 02:08:58 +00001810 return false;
1811
1812 // FIXME: Support vector-GEPs.
1813 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
1814
1815 // If the base pointer is non-null, we cannot walk to a null address with an
1816 // inbounds GEP in address space zero.
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001817 if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q))
Chandler Carruth80d3e562012-12-07 02:08:58 +00001818 return true;
1819
Chandler Carruth80d3e562012-12-07 02:08:58 +00001820 // Walk the GEP operands and see if any operand introduces a non-zero offset.
1821 // If so, then the GEP cannot produce a null pointer, as doing so would
1822 // inherently violate the inbounds contract within address space zero.
1823 for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
1824 GTI != GTE; ++GTI) {
1825 // Struct types are easy -- they must always be indexed by a constant.
Peter Collingbourneab85225b2016-12-02 02:24:42 +00001826 if (StructType *STy = GTI.getStructTypeOrNull()) {
Chandler Carruth80d3e562012-12-07 02:08:58 +00001827 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
1828 unsigned ElementIdx = OpC->getZExtValue();
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001829 const StructLayout *SL = Q.DL.getStructLayout(STy);
Chandler Carruth80d3e562012-12-07 02:08:58 +00001830 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
1831 if (ElementOffset > 0)
1832 return true;
1833 continue;
1834 }
1835
1836 // If we have a zero-sized type, the index doesn't matter. Keep looping.
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001837 if (Q.DL.getTypeAllocSize(GTI.getIndexedType()) == 0)
Chandler Carruth80d3e562012-12-07 02:08:58 +00001838 continue;
1839
1840 // Fast path the constant operand case both for efficiency and so we don't
1841 // increment Depth when just zipping down an all-constant GEP.
1842 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
1843 if (!OpC->isZero())
1844 return true;
1845 continue;
1846 }
1847
1848 // We post-increment Depth here because while isKnownNonZero increments it
1849 // as well, when we pop back up that increment won't persist. We don't want
1850 // to recurse 10k times just because we have 10k GEP operands. We don't
1851 // bail completely out because we want to handle constant GEPs regardless
1852 // of depth.
1853 if (Depth++ >= MaxDepth)
1854 continue;
1855
Matthias Braunfeb81bc2016-01-15 22:22:04 +00001856 if (isKnownNonZero(GTI.getOperand(), Depth, Q))
Chandler Carruth80d3e562012-12-07 02:08:58 +00001857 return true;
1858 }
1859
1860 return false;
1861}
1862
Nuno Lopes404f1062017-09-09 18:23:11 +00001863static bool isKnownNonNullFromDominatingCondition(const Value *V,
1864 const Instruction *CtxI,
1865 const DominatorTree *DT) {
1866 assert(V->getType()->isPointerTy() && "V must be pointer type");
1867 assert(!isa<ConstantData>(V) && "Did not expect ConstantPointerNull");
1868
1869 if (!CtxI || !DT)
1870 return false;
1871
1872 unsigned NumUsesExplored = 0;
1873 for (auto *U : V->users()) {
1874 // Avoid massive lists
1875 if (NumUsesExplored >= DomConditionsMaxUses)
1876 break;
1877 NumUsesExplored++;
1878
1879 // If the value is used as an argument to a call or invoke, then argument
1880 // attributes may provide an answer about null-ness.
1881 if (auto CS = ImmutableCallSite(U))
1882 if (auto *CalledFunc = CS.getCalledFunction())
1883 for (const Argument &Arg : CalledFunc->args())
1884 if (CS.getArgOperand(Arg.getArgNo()) == V &&
1885 Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI))
1886 return true;
1887
1888 // Consider only compare instructions uniquely controlling a branch
1889 CmpInst::Predicate Pred;
1890 if (!match(const_cast<User *>(U),
1891 m_c_ICmp(Pred, m_Specific(V), m_Zero())) ||
1892 (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE))
1893 continue;
1894
Max Kazantsev2dbbd642018-08-06 11:14:18 +00001895 SmallVector<const User *, 4> WorkList;
1896 SmallPtrSet<const User *, 4> Visited;
Nuno Lopes404f1062017-09-09 18:23:11 +00001897 for (auto *CmpU : U->users()) {
Max Kazantsev2dbbd642018-08-06 11:14:18 +00001898 assert(WorkList.empty() && "Should be!");
1899 if (Visited.insert(CmpU).second)
1900 WorkList.push_back(CmpU);
Nuno Lopes404f1062017-09-09 18:23:11 +00001901
Max Kazantsev2dbbd642018-08-06 11:14:18 +00001902 while (!WorkList.empty()) {
1903 auto *Curr = WorkList.pop_back_val();
1904
1905 // If a user is an AND, add all its users to the work list. We only
1906 // propagate "pred != null" condition through AND because it is only
1907 // correct to assume that all conditions of AND are met in true branch.
1908 // TODO: Support similar logic of OR and EQ predicate?
1909 if (Pred == ICmpInst::ICMP_NE)
1910 if (auto *BO = dyn_cast<BinaryOperator>(Curr))
1911 if (BO->getOpcode() == Instruction::And) {
1912 for (auto *BOU : BO->users())
1913 if (Visited.insert(BOU).second)
1914 WorkList.push_back(BOU);
1915 continue;
1916 }
1917
1918 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
1919 assert(BI->isConditional() && "uses a comparison!");
1920
1921 BasicBlock *NonNullSuccessor =
1922 BI->getSuccessor(Pred == ICmpInst::ICMP_EQ ? 1 : 0);
1923 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
1924 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
1925 return true;
Max Kazantsev3c284bd2018-08-30 03:39:16 +00001926 } else if (Pred == ICmpInst::ICMP_NE && isGuard(Curr) &&
Max Kazantsev2dbbd642018-08-06 11:14:18 +00001927 DT->dominates(cast<Instruction>(Curr), CtxI)) {
Nuno Lopes404f1062017-09-09 18:23:11 +00001928 return true;
Max Kazantsev2dbbd642018-08-06 11:14:18 +00001929 }
Nuno Lopes404f1062017-09-09 18:23:11 +00001930 }
1931 }
1932 }
1933
1934 return false;
1935}
1936
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001937/// Does the 'Range' metadata (which must be a valid MD_range operand list)
1938/// ensure that the value it's attached to is never Value? 'RangeType' is
1939/// is the type of the value described by the range.
Pete Cooper35b00d52016-08-13 01:05:32 +00001940static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001941 const unsigned NumRanges = Ranges->getNumOperands() / 2;
1942 assert(NumRanges >= 1);
1943 for (unsigned i = 0; i < NumRanges; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001944 ConstantInt *Lower =
1945 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
1946 ConstantInt *Upper =
1947 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001948 ConstantRange Range(Lower->getValue(), Upper->getValue());
1949 if (Range.contains(Value))
1950 return false;
1951 }
1952 return true;
1953}
1954
Sanjay Patel97e4b9872017-02-12 15:35:34 +00001955/// Return true if the given value is known to be non-zero when defined. For
1956/// vectors, return true if every element is known to be non-zero when
1957/// defined. For pointers, if the context instruction and dominator tree are
1958/// specified, perform context-sensitive analysis and return true if the
1959/// pointer couldn't possibly be null at the specified instruction.
1960/// Supports values with integer or pointer type and vectors of integers.
Pete Cooper35b00d52016-08-13 01:05:32 +00001961bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q) {
Sanjay Patel8ec7e7c2016-05-22 16:07:20 +00001962 if (auto *C = dyn_cast<Constant>(V)) {
Duncan Sandsd3951082011-01-25 09:38:29 +00001963 if (C->isNullValue())
1964 return false;
1965 if (isa<ConstantInt>(C))
1966 // Must be non-zero due to null test above.
1967 return true;
Sanjay Patel23019d12016-05-24 14:18:49 +00001968
1969 // For constant vectors, check that all elements are undefined or known
1970 // non-zero to determine that the whole vector is known non-zero.
1971 if (auto *VecTy = dyn_cast<VectorType>(C->getType())) {
1972 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
1973 Constant *Elt = C->getAggregateElement(i);
1974 if (!Elt || Elt->isNullValue())
1975 return false;
1976 if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt))
1977 return false;
1978 }
1979 return true;
1980 }
1981
Nuno Lopes404f1062017-09-09 18:23:11 +00001982 // A global variable in address space 0 is non null unless extern weak
1983 // or an absolute symbol reference. Other address spaces may have null as a
1984 // valid address for a global, so we can't assume anything.
1985 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1986 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
1987 GV->getType()->getAddressSpace() == 0)
1988 return true;
1989 } else
1990 return false;
Duncan Sandsd3951082011-01-25 09:38:29 +00001991 }
1992
Sanjay Patel8ec7e7c2016-05-22 16:07:20 +00001993 if (auto *I = dyn_cast<Instruction>(V)) {
Florian Hahn19f9e322018-08-17 14:39:04 +00001994 if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) {
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001995 // If the possible ranges don't contain zero, then the value is
1996 // definitely non-zero.
Sanjay Patel8ec7e7c2016-05-22 16:07:20 +00001997 if (auto *Ty = dyn_cast<IntegerType>(V->getType())) {
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001998 const APInt ZeroValue(Ty->getBitWidth(), 0);
1999 if (rangeMetadataExcludesValue(Ranges, ZeroValue))
2000 return true;
2001 }
2002 }
2003 }
2004
Karl-Johan Karlssonebaaa2d2018-05-30 15:56:46 +00002005 // Some of the tests below are recursive, so bail out if we hit the limit.
2006 if (Depth++ >= MaxDepth)
2007 return false;
2008
Nuno Lopes404f1062017-09-09 18:23:11 +00002009 // Check for pointer simplifications.
2010 if (V->getType()->isPointerTy()) {
2011 // Alloca never returns null, malloc might.
2012 if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0)
2013 return true;
2014
2015 // A byval, inalloca, or nonnull argument is never null.
2016 if (const Argument *A = dyn_cast<Argument>(V))
2017 if (A->hasByValOrInAllocaAttr() || A->hasNonNullAttr())
2018 return true;
2019
2020 // A Load tagged with nonnull metadata is never null.
2021 if (const LoadInst *LI = dyn_cast<LoadInst>(V))
Florian Hahn19f9e322018-08-17 14:39:04 +00002022 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull))
Nuno Lopes404f1062017-09-09 18:23:11 +00002023 return true;
2024
Chandler Carruth363ac682019-01-07 05:42:51 +00002025 if (const auto *Call = dyn_cast<CallBase>(V)) {
2026 if (Call->isReturnNonNull())
Nuno Lopes404f1062017-09-09 18:23:11 +00002027 return true;
Chandler Carruth363ac682019-01-07 05:42:51 +00002028 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call))
Karl-Johan Karlssonebaaa2d2018-05-30 15:56:46 +00002029 return isKnownNonZero(RP, Depth, Q);
Piotr Padlewski5642a422018-05-18 23:54:33 +00002030 }
Nuno Lopes404f1062017-09-09 18:23:11 +00002031 }
2032
Duncan Sandsd3951082011-01-25 09:38:29 +00002033
Nuno Lopes404f1062017-09-09 18:23:11 +00002034 // Check for recursive pointer simplifications.
Chandler Carruth80d3e562012-12-07 02:08:58 +00002035 if (V->getType()->isPointerTy()) {
Nuno Lopes404f1062017-09-09 18:23:11 +00002036 if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT))
Sanjoy Das6082c1a2016-05-07 02:08:15 +00002037 return true;
Nuno Lopes404f1062017-09-09 18:23:11 +00002038
Johannes Doerfert00102c72019-01-26 23:40:35 +00002039 // Look through bitcast operations, GEPs, and int2ptr instructions as they
2040 // do not alter the value, or at least not the nullness property of the
2041 // value, e.g., int2ptr is allowed to zero/sign extend the value.
2042 //
2043 // Note that we have to take special care to avoid looking through
2044 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
2045 // as casts that can alter the value, e.g., AddrSpaceCasts.
Pete Cooper35b00d52016-08-13 01:05:32 +00002046 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V))
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002047 if (isGEPKnownNonNull(GEP, Depth, Q))
Chandler Carruth80d3e562012-12-07 02:08:58 +00002048 return true;
Johannes Doerfert00102c72019-01-26 23:40:35 +00002049
2050 if (auto *BCO = dyn_cast<BitCastOperator>(V))
2051 return isKnownNonZero(BCO->getOperand(0), Depth, Q);
2052
2053 if (auto *I2P = dyn_cast<IntToPtrInst>(V))
2054 if (Q.DL.getTypeSizeInBits(I2P->getSrcTy()) <=
2055 Q.DL.getTypeSizeInBits(I2P->getDestTy()))
2056 return isKnownNonZero(I2P->getOperand(0), Depth, Q);
Chandler Carruth80d3e562012-12-07 02:08:58 +00002057 }
2058
Johannes Doerfert00102c72019-01-26 23:40:35 +00002059 // Similar to int2ptr above, we can look through ptr2int here if the cast
2060 // is a no-op or an extend and not a truncate.
2061 if (auto *P2I = dyn_cast<PtrToIntInst>(V))
2062 if (Q.DL.getTypeSizeInBits(P2I->getSrcTy()) <=
2063 Q.DL.getTypeSizeInBits(P2I->getDestTy()))
2064 return isKnownNonZero(P2I->getOperand(0), Depth, Q);
2065
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002066 unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), Q.DL);
Duncan Sandsd3951082011-01-25 09:38:29 +00002067
2068 // X | Y != 0 if X != 0 or Y != 0.
Craig Topper9f008862014-04-15 04:59:12 +00002069 Value *X = nullptr, *Y = nullptr;
Duncan Sandsd3951082011-01-25 09:38:29 +00002070 if (match(V, m_Or(m_Value(X), m_Value(Y))))
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002071 return isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00002072
2073 // ext X != 0 if X != 0.
2074 if (isa<SExtInst>(V) || isa<ZExtInst>(V))
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002075 return isKnownNonZero(cast<Instruction>(V)->getOperand(0), Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00002076
Duncan Sands2e9e4f12011-01-29 13:27:00 +00002077 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
Duncan Sandsd3951082011-01-25 09:38:29 +00002078 // if the lowest bit is shifted off the end.
Craig Topper6b3940a2017-05-03 22:25:19 +00002079 if (match(V, m_Shl(m_Value(X), m_Value(Y)))) {
Nick Lewyckyc9aab852011-02-28 08:02:21 +00002080 // shl nuw can't remove any non-zero bits.
Pete Cooper35b00d52016-08-13 01:05:32 +00002081 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
Florian Hahn19f9e322018-08-17 14:39:04 +00002082 if (Q.IIQ.hasNoUnsignedWrap(BO))
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002083 return isKnownNonZero(X, Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00002084
Craig Topperb45eabc2017-04-26 16:39:58 +00002085 KnownBits Known(BitWidth);
2086 computeKnownBits(X, Known, Depth, Q);
2087 if (Known.One[0])
Duncan Sandsd3951082011-01-25 09:38:29 +00002088 return true;
2089 }
Duncan Sands2e9e4f12011-01-29 13:27:00 +00002090 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
Duncan Sandsd3951082011-01-25 09:38:29 +00002091 // defined if the sign bit is shifted off the end.
2092 else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) {
Nick Lewyckyc9aab852011-02-28 08:02:21 +00002093 // shr exact can only shift out zero bits.
Pete Cooper35b00d52016-08-13 01:05:32 +00002094 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00002095 if (BO->isExact())
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002096 return isKnownNonZero(X, Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00002097
Craig Topper6e11a052017-05-08 16:22:48 +00002098 KnownBits Known = computeKnownBits(X, Depth, Q);
2099 if (Known.isNegative())
Duncan Sandsd3951082011-01-25 09:38:29 +00002100 return true;
James Molloyb6be1eb2015-09-24 16:06:32 +00002101
2102 // If the shifter operand is a constant, and all of the bits shifted
2103 // out are known to be zero, and X is known non-zero then at least one
2104 // non-zero bit must remain.
2105 if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y)) {
James Molloyb6be1eb2015-09-24 16:06:32 +00002106 auto ShiftVal = Shift->getLimitedValue(BitWidth - 1);
2107 // Is there a known one in the portion not shifted out?
Craig Topper8df66c62017-05-12 17:20:30 +00002108 if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal)
James Molloyb6be1eb2015-09-24 16:06:32 +00002109 return true;
2110 // Are all the bits to be shifted out known zero?
NAKAMURA Takumi76bab1f2017-07-11 02:31:51 +00002111 if (Known.countMinTrailingZeros() >= ShiftVal)
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002112 return isKnownNonZero(X, Depth, Q);
James Molloyb6be1eb2015-09-24 16:06:32 +00002113 }
Duncan Sandsd3951082011-01-25 09:38:29 +00002114 }
Nick Lewyckyc9aab852011-02-28 08:02:21 +00002115 // div exact can only produce a zero if the dividend is zero.
Benjamin Kramer9442cd02012-01-01 17:55:30 +00002116 else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002117 return isKnownNonZero(X, Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00002118 }
Duncan Sandsd3951082011-01-25 09:38:29 +00002119 // X + Y.
2120 else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
Craig Topper6e11a052017-05-08 16:22:48 +00002121 KnownBits XKnown = computeKnownBits(X, Depth, Q);
2122 KnownBits YKnown = computeKnownBits(Y, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00002123
2124 // If X and Y are both non-negative (as signed values) then their sum is not
Duncan Sands9e9d5b22011-01-25 15:14:15 +00002125 // zero unless both X and Y are zero.
Craig Topper6e11a052017-05-08 16:22:48 +00002126 if (XKnown.isNonNegative() && YKnown.isNonNegative())
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002127 if (isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q))
Duncan Sands9e9d5b22011-01-25 15:14:15 +00002128 return true;
Duncan Sandsd3951082011-01-25 09:38:29 +00002129
2130 // If X and Y are both negative (as signed values) then their sum is not
2131 // zero unless both X and Y equal INT_MIN.
Craig Topper6e11a052017-05-08 16:22:48 +00002132 if (XKnown.isNegative() && YKnown.isNegative()) {
Duncan Sandsd3951082011-01-25 09:38:29 +00002133 APInt Mask = APInt::getSignedMaxValue(BitWidth);
2134 // The sign bit of X is set. If some other bit is set then X is not equal
2135 // to INT_MIN.
Craig Topper6e11a052017-05-08 16:22:48 +00002136 if (XKnown.One.intersects(Mask))
Duncan Sandsd3951082011-01-25 09:38:29 +00002137 return true;
2138 // The sign bit of Y is set. If some other bit is set then Y is not equal
2139 // to INT_MIN.
Craig Topper6e11a052017-05-08 16:22:48 +00002140 if (YKnown.One.intersects(Mask))
Duncan Sandsd3951082011-01-25 09:38:29 +00002141 return true;
2142 }
2143
2144 // The sum of a non-negative number and a power of two is not zero.
Craig Topper6e11a052017-05-08 16:22:48 +00002145 if (XKnown.isNonNegative() &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002146 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q))
Duncan Sandsd3951082011-01-25 09:38:29 +00002147 return true;
Craig Topper6e11a052017-05-08 16:22:48 +00002148 if (YKnown.isNonNegative() &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002149 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
Duncan Sandsd3951082011-01-25 09:38:29 +00002150 return true;
2151 }
Duncan Sands7cb61e52011-10-27 19:16:21 +00002152 // X * Y.
2153 else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
Pete Cooper35b00d52016-08-13 01:05:32 +00002154 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
Duncan Sands7cb61e52011-10-27 19:16:21 +00002155 // If X and Y are non-zero then so is X * Y as long as the multiplication
2156 // does not overflow.
Florian Hahn19f9e322018-08-17 14:39:04 +00002157 if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) &&
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002158 isKnownNonZero(X, Depth, Q) && isKnownNonZero(Y, Depth, Q))
Duncan Sands7cb61e52011-10-27 19:16:21 +00002159 return true;
2160 }
Duncan Sandsd3951082011-01-25 09:38:29 +00002161 // (C ? X : Y) != 0 if X != 0 and Y != 0.
Pete Cooper35b00d52016-08-13 01:05:32 +00002162 else if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002163 if (isKnownNonZero(SI->getTrueValue(), Depth, Q) &&
2164 isKnownNonZero(SI->getFalseValue(), Depth, Q))
Duncan Sandsd3951082011-01-25 09:38:29 +00002165 return true;
2166 }
James Molloy897048b2015-09-29 14:08:45 +00002167 // PHI
Pete Cooper35b00d52016-08-13 01:05:32 +00002168 else if (const PHINode *PN = dyn_cast<PHINode>(V)) {
James Molloy897048b2015-09-29 14:08:45 +00002169 // Try and detect a recurrence that monotonically increases from a
2170 // starting value, as these are common as induction variables.
2171 if (PN->getNumIncomingValues() == 2) {
2172 Value *Start = PN->getIncomingValue(0);
2173 Value *Induction = PN->getIncomingValue(1);
2174 if (isa<ConstantInt>(Induction) && !isa<ConstantInt>(Start))
2175 std::swap(Start, Induction);
2176 if (ConstantInt *C = dyn_cast<ConstantInt>(Start)) {
2177 if (!C->isZero() && !C->isNegative()) {
2178 ConstantInt *X;
Florian Hahn19f9e322018-08-17 14:39:04 +00002179 if (Q.IIQ.UseInstrInfo &&
2180 (match(Induction, m_NSWAdd(m_Specific(PN), m_ConstantInt(X))) ||
James Molloy897048b2015-09-29 14:08:45 +00002181 match(Induction, m_NUWAdd(m_Specific(PN), m_ConstantInt(X)))) &&
2182 !X->isNegative())
2183 return true;
2184 }
2185 }
2186 }
Jun Bum Limca832662016-02-01 17:03:07 +00002187 // Check if all incoming values are non-zero constant.
Eugene Zelenko75075ef2017-09-01 21:37:29 +00002188 bool AllNonZeroConstants = llvm::all_of(PN->operands(), [](Value *V) {
Craig Topper79ab6432017-07-06 18:39:47 +00002189 return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZero();
Jun Bum Limca832662016-02-01 17:03:07 +00002190 });
2191 if (AllNonZeroConstants)
2192 return true;
James Molloy897048b2015-09-29 14:08:45 +00002193 }
Duncan Sandsd3951082011-01-25 09:38:29 +00002194
Craig Topperb45eabc2017-04-26 16:39:58 +00002195 KnownBits Known(BitWidth);
2196 computeKnownBits(V, Known, Depth, Q);
2197 return Known.One != 0;
Duncan Sandsd3951082011-01-25 09:38:29 +00002198}
2199
James Molloy1d88d6f2015-10-22 13:18:42 +00002200/// Return true if V2 == V1 + X, where X is known non-zero.
Pete Cooper35b00d52016-08-13 01:05:32 +00002201static bool isAddOfNonZero(const Value *V1, const Value *V2, const Query &Q) {
2202 const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
James Molloy1d88d6f2015-10-22 13:18:42 +00002203 if (!BO || BO->getOpcode() != Instruction::Add)
2204 return false;
2205 Value *Op = nullptr;
2206 if (V2 == BO->getOperand(0))
2207 Op = BO->getOperand(1);
2208 else if (V2 == BO->getOperand(1))
2209 Op = BO->getOperand(0);
2210 else
2211 return false;
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002212 return isKnownNonZero(Op, 0, Q);
James Molloy1d88d6f2015-10-22 13:18:42 +00002213}
2214
2215/// Return true if it is known that V1 != V2.
Pete Cooper35b00d52016-08-13 01:05:32 +00002216static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q) {
Craig Topper3002d5b2017-06-06 07:13:15 +00002217 if (V1 == V2)
James Molloy1d88d6f2015-10-22 13:18:42 +00002218 return false;
2219 if (V1->getType() != V2->getType())
2220 // We can't look through casts yet.
2221 return false;
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002222 if (isAddOfNonZero(V1, V2, Q) || isAddOfNonZero(V2, V1, Q))
James Molloy1d88d6f2015-10-22 13:18:42 +00002223 return true;
2224
Craig Topper3002d5b2017-06-06 07:13:15 +00002225 if (V1->getType()->isIntOrIntVectorTy()) {
James Molloy1d88d6f2015-10-22 13:18:42 +00002226 // Are any known bits in V1 contradictory to known bits in V2? If V1
2227 // has a known zero where V2 has a known one, they must not be equal.
Craig Topper8e662f72017-06-06 07:13:11 +00002228 KnownBits Known1 = computeKnownBits(V1, 0, Q);
2229 KnownBits Known2 = computeKnownBits(V2, 0, Q);
James Molloy1d88d6f2015-10-22 13:18:42 +00002230
Craig Topper8365df82017-06-06 07:13:09 +00002231 if (Known1.Zero.intersects(Known2.One) ||
2232 Known2.Zero.intersects(Known1.One))
James Molloy1d88d6f2015-10-22 13:18:42 +00002233 return true;
2234 }
2235 return false;
2236}
2237
Sanjay Patelaee84212014-11-04 16:27:42 +00002238/// Return true if 'V & Mask' is known to be zero. We use this predicate to
2239/// simplify operations downstream. Mask is known to be zero for bits that V
2240/// cannot have.
Chris Lattner4bc28252009-09-08 00:06:16 +00002241///
2242/// This function is defined on values with integer type, values with pointer
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002243/// type, and vectors of integers. In the case
Chris Lattner4bc28252009-09-08 00:06:16 +00002244/// where V is a vector, the mask, known zero, and known one values are the
2245/// same width as the vector element, and the bit is set only if it is true
2246/// for all of the elements in the vector.
Pete Cooper35b00d52016-08-13 01:05:32 +00002247bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002248 const Query &Q) {
Craig Topperb45eabc2017-04-26 16:39:58 +00002249 KnownBits Known(Mask.getBitWidth());
2250 computeKnownBits(V, Known, Depth, Q);
2251 return Mask.isSubsetOf(Known.Zero);
Chris Lattner965c7692008-06-02 01:18:21 +00002252}
2253
Craig Topperbec15b62018-08-22 23:27:50 +00002254// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
2255// Returns the input and lower/upper bounds.
2256static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
2257 const APInt *&CLow, const APInt *&CHigh) {
Craig Topper15f86922018-08-23 17:15:02 +00002258 assert(isa<Operator>(Select) &&
2259 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
Craig Topperdfa176e2018-08-23 17:45:53 +00002260 "Input should be a Select!");
Craig Topperbec15b62018-08-22 23:27:50 +00002261
2262 const Value *LHS, *RHS, *LHS2, *RHS2;
2263 SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor;
2264 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
2265 return false;
2266
2267 if (!match(RHS, m_APInt(CLow)))
2268 return false;
2269
2270 SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor;
2271 if (getInverseMinMaxFlavor(SPF) != SPF2)
2272 return false;
2273
2274 if (!match(RHS2, m_APInt(CHigh)))
2275 return false;
2276
2277 if (SPF == SPF_SMIN)
2278 std::swap(CLow, CHigh);
2279
2280 In = LHS2;
2281 return CLow->sle(*CHigh);
2282}
2283
Sanjay Patela06d9892016-06-22 19:20:59 +00002284/// For vector constants, loop over the elements and find the constant with the
2285/// minimum number of sign bits. Return 0 if the value is not a vector constant
2286/// or if any element was not analyzed; otherwise, return the count for the
2287/// element with the minimum number of sign bits.
Pete Cooper35b00d52016-08-13 01:05:32 +00002288static unsigned computeNumSignBitsVectorConstant(const Value *V,
2289 unsigned TyBits) {
2290 const auto *CV = dyn_cast<Constant>(V);
Sanjay Patela06d9892016-06-22 19:20:59 +00002291 if (!CV || !CV->getType()->isVectorTy())
2292 return 0;
Chris Lattner965c7692008-06-02 01:18:21 +00002293
Sanjay Patela06d9892016-06-22 19:20:59 +00002294 unsigned MinSignBits = TyBits;
2295 unsigned NumElts = CV->getType()->getVectorNumElements();
2296 for (unsigned i = 0; i != NumElts; ++i) {
2297 // If we find a non-ConstantInt, bail out.
2298 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
2299 if (!Elt)
2300 return 0;
2301
Craig Topper8e8b6ef2017-10-21 16:35:41 +00002302 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
Sanjay Patela06d9892016-06-22 19:20:59 +00002303 }
2304
2305 return MinSignBits;
2306}
Chris Lattner965c7692008-06-02 01:18:21 +00002307
Sanjoy Das39a684d2017-02-25 20:30:45 +00002308static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth,
2309 const Query &Q);
2310
2311static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
2312 const Query &Q) {
2313 unsigned Result = ComputeNumSignBitsImpl(V, Depth, Q);
2314 assert(Result > 0 && "At least one sign bit needs to be present!");
2315 return Result;
2316}
2317
Sanjay Patelaee84212014-11-04 16:27:42 +00002318/// Return the number of times the sign bit of the register is replicated into
2319/// the other bits. We know that at least 1 bit is always equal to the sign bit
2320/// (itself), but other cases can give us information. For example, immediately
2321/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
Sanjay Patela06d9892016-06-22 19:20:59 +00002322/// other, so we return 3. For vectors, return the number of sign bits for the
Vedant Kumard3196742018-02-28 19:08:52 +00002323/// vector element with the minimum number of known sign bits.
Sanjoy Das39a684d2017-02-25 20:30:45 +00002324static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth,
2325 const Query &Q) {
Craig Topper7227eba2017-08-21 22:56:12 +00002326 assert(Depth <= MaxDepth && "Limit Search Depth");
Sanjoy Das39a684d2017-02-25 20:30:45 +00002327
2328 // We return the minimum number of sign bits that are guaranteed to be present
2329 // in V, so for undef we have to conservatively return 1. We don't have the
2330 // same behavior for poison though -- that's a FIXME today.
2331
Elena Demikhovsky945b7e52018-02-14 06:58:08 +00002332 Type *ScalarTy = V->getType()->getScalarType();
2333 unsigned TyBits = ScalarTy->isPointerTy() ?
2334 Q.DL.getIndexTypeSizeInBits(ScalarTy) :
2335 Q.DL.getTypeSizeInBits(ScalarTy);
2336
Chris Lattner965c7692008-06-02 01:18:21 +00002337 unsigned Tmp, Tmp2;
2338 unsigned FirstAnswer = 1;
2339
Jay Foada0653a32014-05-14 21:14:37 +00002340 // Note that ConstantInt is handled by the general computeKnownBits case
Chris Lattner2e01a692008-06-02 18:39:07 +00002341 // below.
2342
Matt Arsenaultcb2a7eb2016-12-20 19:06:15 +00002343 if (Depth == MaxDepth)
Chris Lattner965c7692008-06-02 01:18:21 +00002344 return 1; // Limit search depth.
Craig Topper1bef2c82012-12-22 19:15:35 +00002345
Pete Cooper35b00d52016-08-13 01:05:32 +00002346 const Operator *U = dyn_cast<Operator>(V);
Dan Gohman80ca01c2009-07-17 20:47:02 +00002347 switch (Operator::getOpcode(V)) {
Chris Lattner965c7692008-06-02 01:18:21 +00002348 default: break;
2349 case Instruction::SExt:
Mon P Wangbb3eac92009-12-02 04:59:58 +00002350 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002351 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp;
Craig Topper1bef2c82012-12-22 19:15:35 +00002352
Nadav Rotemc99a3872015-03-06 00:23:58 +00002353 case Instruction::SDiv: {
Nadav Rotem029c5c72015-03-03 21:39:02 +00002354 const APInt *Denominator;
2355 // sdiv X, C -> adds log(C) sign bits.
2356 if (match(U->getOperand(1), m_APInt(Denominator))) {
2357
2358 // Ignore non-positive denominator.
2359 if (!Denominator->isStrictlyPositive())
2360 break;
2361
2362 // Calculate the incoming numerator bits.
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002363 unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
Nadav Rotem029c5c72015-03-03 21:39:02 +00002364
2365 // Add floor(log(C)) bits to the numerator bits.
2366 return std::min(TyBits, NumBits + Denominator->logBase2());
2367 }
2368 break;
Nadav Rotemc99a3872015-03-06 00:23:58 +00002369 }
2370
2371 case Instruction::SRem: {
2372 const APInt *Denominator;
Sanjoy Dase561fee2015-03-25 22:33:53 +00002373 // srem X, C -> we know that the result is within [-C+1,C) when C is a
2374 // positive constant. This let us put a lower bound on the number of sign
2375 // bits.
Nadav Rotemc99a3872015-03-06 00:23:58 +00002376 if (match(U->getOperand(1), m_APInt(Denominator))) {
2377
2378 // Ignore non-positive denominator.
2379 if (!Denominator->isStrictlyPositive())
2380 break;
2381
2382 // Calculate the incoming numerator bits. SRem by a positive constant
2383 // can't lower the number of sign bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002384 unsigned NumrBits =
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002385 ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
Nadav Rotemc99a3872015-03-06 00:23:58 +00002386
2387 // Calculate the leading sign bit constraints by examining the
Sanjoy Dase561fee2015-03-25 22:33:53 +00002388 // denominator. Given that the denominator is positive, there are two
2389 // cases:
2390 //
2391 // 1. the numerator is positive. The result range is [0,C) and [0,C) u<
2392 // (1 << ceilLogBase2(C)).
2393 //
2394 // 2. the numerator is negative. Then the result range is (-C,0] and
2395 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
2396 //
2397 // Thus a lower bound on the number of sign bits is `TyBits -
2398 // ceilLogBase2(C)`.
Nadav Rotemc99a3872015-03-06 00:23:58 +00002399
Sanjoy Dase561fee2015-03-25 22:33:53 +00002400 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
Nadav Rotemc99a3872015-03-06 00:23:58 +00002401 return std::max(NumrBits, ResBits);
2402 }
2403 break;
2404 }
Nadav Rotem029c5c72015-03-03 21:39:02 +00002405
Chris Lattner61a1d6c2012-01-26 21:37:55 +00002406 case Instruction::AShr: {
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002407 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
Chris Lattner61a1d6c2012-01-26 21:37:55 +00002408 // ashr X, C -> adds C sign bits. Vectors too.
2409 const APInt *ShAmt;
2410 if (match(U->getOperand(1), m_APInt(ShAmt))) {
Simon Pilgrim67207262018-01-01 22:44:59 +00002411 if (ShAmt->uge(TyBits))
Sanjoy Das39a684d2017-02-25 20:30:45 +00002412 break; // Bad shift.
Simon Pilgrim67207262018-01-01 22:44:59 +00002413 unsigned ShAmtLimited = ShAmt->getZExtValue();
Sanjoy Das39a684d2017-02-25 20:30:45 +00002414 Tmp += ShAmtLimited;
Chris Lattner965c7692008-06-02 01:18:21 +00002415 if (Tmp > TyBits) Tmp = TyBits;
2416 }
2417 return Tmp;
Chris Lattner61a1d6c2012-01-26 21:37:55 +00002418 }
2419 case Instruction::Shl: {
2420 const APInt *ShAmt;
2421 if (match(U->getOperand(1), m_APInt(ShAmt))) {
Chris Lattner965c7692008-06-02 01:18:21 +00002422 // shl destroys sign bits.
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002423 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
Simon Pilgrim67207262018-01-01 22:44:59 +00002424 if (ShAmt->uge(TyBits) || // Bad shift.
2425 ShAmt->uge(Tmp)) break; // Shifted all sign bits out.
Chris Lattner61a1d6c2012-01-26 21:37:55 +00002426 Tmp2 = ShAmt->getZExtValue();
Chris Lattner61a1d6c2012-01-26 21:37:55 +00002427 return Tmp - Tmp2;
Chris Lattner965c7692008-06-02 01:18:21 +00002428 }
2429 break;
Chris Lattner61a1d6c2012-01-26 21:37:55 +00002430 }
Chris Lattner965c7692008-06-02 01:18:21 +00002431 case Instruction::And:
2432 case Instruction::Or:
2433 case Instruction::Xor: // NOT is handled here.
2434 // Logical binary ops preserve the number of sign bits at the worst.
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002435 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002436 if (Tmp != 1) {
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002437 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002438 FirstAnswer = std::min(Tmp, Tmp2);
2439 // We computed what we know about the sign bits as our first
2440 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002441 // computeKnownBits, and pick whichever answer is better.
Chris Lattner965c7692008-06-02 01:18:21 +00002442 }
2443 break;
2444
Craig Topperbec15b62018-08-22 23:27:50 +00002445 case Instruction::Select: {
2446 // If we have a clamp pattern, we know that the number of sign bits will be
2447 // the minimum of the clamp min/max range.
2448 const Value *X;
2449 const APInt *CLow, *CHigh;
2450 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
2451 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
2452
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002453 Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
Stanislav Mekhanoshinb8269a92018-07-25 16:39:24 +00002454 if (Tmp == 1) break;
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002455 Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002456 return std::min(Tmp, Tmp2);
Craig Topperbec15b62018-08-22 23:27:50 +00002457 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002458
Chris Lattner965c7692008-06-02 01:18:21 +00002459 case Instruction::Add:
2460 // Add can have at most one carry bit. Thus we know that the output
2461 // is, at worst, one more bit than the inputs.
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002462 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
Stanislav Mekhanoshinb8269a92018-07-25 16:39:24 +00002463 if (Tmp == 1) break;
Craig Topper1bef2c82012-12-22 19:15:35 +00002464
Chris Lattner965c7692008-06-02 01:18:21 +00002465 // Special case decrementing a value (ADD X, -1):
David Majnemera55027f2014-12-26 09:20:17 +00002466 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
Chris Lattner965c7692008-06-02 01:18:21 +00002467 if (CRHS->isAllOnesValue()) {
Craig Topperb45eabc2017-04-26 16:39:58 +00002468 KnownBits Known(TyBits);
2469 computeKnownBits(U->getOperand(0), Known, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00002470
Chris Lattner965c7692008-06-02 01:18:21 +00002471 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2472 // sign bits set.
Craig Topperb45eabc2017-04-26 16:39:58 +00002473 if ((Known.Zero | 1).isAllOnesValue())
Chris Lattner965c7692008-06-02 01:18:21 +00002474 return TyBits;
Craig Topper1bef2c82012-12-22 19:15:35 +00002475
Chris Lattner965c7692008-06-02 01:18:21 +00002476 // If we are subtracting one from a positive number, there is no carry
2477 // out of the result.
Craig Topperca48af32017-04-29 16:43:11 +00002478 if (Known.isNonNegative())
Chris Lattner965c7692008-06-02 01:18:21 +00002479 return Tmp;
2480 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002481
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002482 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
Stanislav Mekhanoshinb8269a92018-07-25 16:39:24 +00002483 if (Tmp2 == 1) break;
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002484 return std::min(Tmp, Tmp2)-1;
Craig Topper1bef2c82012-12-22 19:15:35 +00002485
Chris Lattner965c7692008-06-02 01:18:21 +00002486 case Instruction::Sub:
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002487 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
Stanislav Mekhanoshinb8269a92018-07-25 16:39:24 +00002488 if (Tmp2 == 1) break;
Craig Topper1bef2c82012-12-22 19:15:35 +00002489
Chris Lattner965c7692008-06-02 01:18:21 +00002490 // Handle NEG.
David Majnemera55027f2014-12-26 09:20:17 +00002491 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
Chris Lattner965c7692008-06-02 01:18:21 +00002492 if (CLHS->isNullValue()) {
Craig Topperb45eabc2017-04-26 16:39:58 +00002493 KnownBits Known(TyBits);
2494 computeKnownBits(U->getOperand(1), Known, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002495 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2496 // sign bits set.
Craig Topperb45eabc2017-04-26 16:39:58 +00002497 if ((Known.Zero | 1).isAllOnesValue())
Chris Lattner965c7692008-06-02 01:18:21 +00002498 return TyBits;
Craig Topper1bef2c82012-12-22 19:15:35 +00002499
Chris Lattner965c7692008-06-02 01:18:21 +00002500 // If the input is known to be positive (the sign bit is known clear),
2501 // the output of the NEG has the same number of sign bits as the input.
Craig Topperca48af32017-04-29 16:43:11 +00002502 if (Known.isNonNegative())
Chris Lattner965c7692008-06-02 01:18:21 +00002503 return Tmp2;
Craig Topper1bef2c82012-12-22 19:15:35 +00002504
Chris Lattner965c7692008-06-02 01:18:21 +00002505 // Otherwise, we treat this like a SUB.
2506 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002507
Chris Lattner965c7692008-06-02 01:18:21 +00002508 // Sub can have at most one carry bit. Thus we know that the output
2509 // is, at worst, one more bit than the inputs.
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002510 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
Stanislav Mekhanoshinb8269a92018-07-25 16:39:24 +00002511 if (Tmp == 1) break;
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002512 return std::min(Tmp, Tmp2)-1;
Craig Topper1bef2c82012-12-22 19:15:35 +00002513
Amjad Aboud88ffa3a2017-08-18 22:56:55 +00002514 case Instruction::Mul: {
2515 // The output of the Mul can be at most twice the valid bits in the inputs.
2516 unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
Stanislav Mekhanoshinb8269a92018-07-25 16:39:24 +00002517 if (SignBitsOp0 == 1) break;
Amjad Aboud88ffa3a2017-08-18 22:56:55 +00002518 unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
Stanislav Mekhanoshinb8269a92018-07-25 16:39:24 +00002519 if (SignBitsOp1 == 1) break;
Amjad Aboud88ffa3a2017-08-18 22:56:55 +00002520 unsigned OutValidBits =
2521 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
2522 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
2523 }
2524
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002525 case Instruction::PHI: {
Pete Cooper35b00d52016-08-13 01:05:32 +00002526 const PHINode *PN = cast<PHINode>(U);
David Majnemer6ee8d172015-01-04 07:06:53 +00002527 unsigned NumIncomingValues = PN->getNumIncomingValues();
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002528 // Don't analyze large in-degree PHIs.
David Majnemer6ee8d172015-01-04 07:06:53 +00002529 if (NumIncomingValues > 4) break;
2530 // Unreachable blocks may have zero-operand PHI nodes.
2531 if (NumIncomingValues == 0) break;
Craig Topper1bef2c82012-12-22 19:15:35 +00002532
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002533 // Take the minimum of all incoming values. This can't infinitely loop
2534 // because of our depth threshold.
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002535 Tmp = ComputeNumSignBits(PN->getIncomingValue(0), Depth + 1, Q);
David Majnemer6ee8d172015-01-04 07:06:53 +00002536 for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) {
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002537 if (Tmp == 1) return Tmp;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002538 Tmp = std::min(
Matthias Braunfeb81bc2016-01-15 22:22:04 +00002539 Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, Q));
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002540 }
2541 return Tmp;
2542 }
2543
Chris Lattner965c7692008-06-02 01:18:21 +00002544 case Instruction::Trunc:
2545 // FIXME: it's tricky to do anything useful for this, but it is an important
2546 // case for targets like X86.
2547 break;
Bjorn Pettersson39616032016-10-06 09:56:21 +00002548
2549 case Instruction::ExtractElement:
2550 // Look through extract element. At the moment we keep this simple and skip
2551 // tracking the specific element. But at least we might find information
2552 // valid for all elements of the vector (for example if vector is sign
2553 // extended, shifted, etc).
2554 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
Sanjay Patelcc9e4012018-10-26 21:05:14 +00002555
Sanjay Patela68096c2018-11-02 15:51:47 +00002556 case Instruction::ShuffleVector: {
Sanjay Patelcac28b42018-11-03 13:18:55 +00002557 // TODO: This is copied almost directly from the SelectionDAG version of
2558 // ComputeNumSignBits. It would be better if we could share common
2559 // code. If not, make sure that changes are translated to the DAG.
2560
2561 // Collect the minimum number of sign bits that are shared by every vector
2562 // element referenced by the shuffle.
2563 auto *Shuf = cast<ShuffleVectorInst>(U);
2564 int NumElts = Shuf->getOperand(0)->getType()->getVectorNumElements();
2565 int NumMaskElts = Shuf->getMask()->getType()->getVectorNumElements();
2566 APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0);
2567 for (int i = 0; i != NumMaskElts; ++i) {
2568 int M = Shuf->getMaskValue(i);
2569 assert(M < NumElts * 2 && "Invalid shuffle mask constant");
2570 // For undef elements, we don't know anything about the common state of
2571 // the shuffle result.
2572 if (M == -1)
2573 return 1;
2574 if (M < NumElts)
2575 DemandedLHS.setBit(M % NumElts);
2576 else
2577 DemandedRHS.setBit(M % NumElts);
2578 }
2579 Tmp = std::numeric_limits<unsigned>::max();
2580 if (!!DemandedLHS)
2581 Tmp = ComputeNumSignBits(Shuf->getOperand(0), Depth + 1, Q);
2582 if (!!DemandedRHS) {
2583 Tmp2 = ComputeNumSignBits(Shuf->getOperand(1), Depth + 1, Q);
2584 Tmp = std::min(Tmp, Tmp2);
2585 }
2586 // If we don't know anything, early out and try computeKnownBits fall-back.
2587 if (Tmp == 1)
Sanjay Patelcc9e4012018-10-26 21:05:14 +00002588 break;
Sanjay Patelcac28b42018-11-03 13:18:55 +00002589 assert(Tmp <= V->getType()->getScalarSizeInBits() &&
2590 "Failed to determine minimum sign bits");
2591 return Tmp;
Chris Lattner965c7692008-06-02 01:18:21 +00002592 }
Sanjay Patela68096c2018-11-02 15:51:47 +00002593 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002594
Chris Lattner965c7692008-06-02 01:18:21 +00002595 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2596 // use this information.
Sanjay Patela06d9892016-06-22 19:20:59 +00002597
2598 // If we can examine all elements of a vector constant successfully, we're
2599 // done (we can't do any better than that). If not, keep trying.
2600 if (unsigned VecSignBits = computeNumSignBitsVectorConstant(V, TyBits))
2601 return VecSignBits;
2602
Craig Topperb45eabc2017-04-26 16:39:58 +00002603 KnownBits Known(TyBits);
2604 computeKnownBits(V, Known, Depth, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00002605
Sanjay Patele0536212016-06-23 17:41:59 +00002606 // If we know that the sign bit is either zero or one, determine the number of
2607 // identical bits in the top of the input value.
Craig Topper8df66c62017-05-12 17:20:30 +00002608 return std::max(FirstAnswer, Known.countMinSignBits());
Chris Lattner965c7692008-06-02 01:18:21 +00002609}
Chris Lattnera12a6de2008-06-02 01:29:46 +00002610
Sanjay Patelaee84212014-11-04 16:27:42 +00002611/// This function computes the integer multiple of Base that equals V.
2612/// If successful, it returns true and returns the multiple in
2613/// Multiple. If unsuccessful, it returns false. It looks
Victor Hernandez47444882009-11-10 08:28:35 +00002614/// through SExt instructions only if LookThroughSExt is true.
2615bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
Dan Gohman6a976bb2009-11-18 00:58:27 +00002616 bool LookThroughSExt, unsigned Depth) {
Victor Hernandez47444882009-11-10 08:28:35 +00002617 const unsigned MaxDepth = 6;
2618
Dan Gohman6a976bb2009-11-18 00:58:27 +00002619 assert(V && "No Value?");
Victor Hernandez47444882009-11-10 08:28:35 +00002620 assert(Depth <= MaxDepth && "Limit Search Depth");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002621 assert(V->getType()->isIntegerTy() && "Not integer or pointer type!");
Victor Hernandez47444882009-11-10 08:28:35 +00002622
Chris Lattner229907c2011-07-18 04:54:35 +00002623 Type *T = V->getType();
Victor Hernandez47444882009-11-10 08:28:35 +00002624
Dan Gohman6a976bb2009-11-18 00:58:27 +00002625 ConstantInt *CI = dyn_cast<ConstantInt>(V);
Victor Hernandez47444882009-11-10 08:28:35 +00002626
2627 if (Base == 0)
2628 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002629
Victor Hernandez47444882009-11-10 08:28:35 +00002630 if (Base == 1) {
2631 Multiple = V;
2632 return true;
2633 }
2634
2635 ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
2636 Constant *BaseVal = ConstantInt::get(T, Base);
2637 if (CO && CO == BaseVal) {
2638 // Multiple is 1.
2639 Multiple = ConstantInt::get(T, 1);
2640 return true;
2641 }
2642
2643 if (CI && CI->getZExtValue() % Base == 0) {
2644 Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
Craig Topper1bef2c82012-12-22 19:15:35 +00002645 return true;
Victor Hernandez47444882009-11-10 08:28:35 +00002646 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002647
Victor Hernandez47444882009-11-10 08:28:35 +00002648 if (Depth == MaxDepth) return false; // Limit search depth.
Craig Topper1bef2c82012-12-22 19:15:35 +00002649
Victor Hernandez47444882009-11-10 08:28:35 +00002650 Operator *I = dyn_cast<Operator>(V);
2651 if (!I) return false;
2652
2653 switch (I->getOpcode()) {
2654 default: break;
Chris Lattner4f0b47d2009-11-26 01:50:12 +00002655 case Instruction::SExt:
Victor Hernandez47444882009-11-10 08:28:35 +00002656 if (!LookThroughSExt) return false;
2657 // otherwise fall through to ZExt
Galina Kistanova244621f2017-05-31 22:16:24 +00002658 LLVM_FALLTHROUGH;
Chris Lattner4f0b47d2009-11-26 01:50:12 +00002659 case Instruction::ZExt:
Dan Gohman6a976bb2009-11-18 00:58:27 +00002660 return ComputeMultiple(I->getOperand(0), Base, Multiple,
2661 LookThroughSExt, Depth+1);
Victor Hernandez47444882009-11-10 08:28:35 +00002662 case Instruction::Shl:
2663 case Instruction::Mul: {
2664 Value *Op0 = I->getOperand(0);
2665 Value *Op1 = I->getOperand(1);
2666
2667 if (I->getOpcode() == Instruction::Shl) {
2668 ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
2669 if (!Op1CI) return false;
2670 // Turn Op0 << Op1 into Op0 * 2^Op1
2671 APInt Op1Int = Op1CI->getValue();
2672 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
Jay Foad15084f02010-11-30 09:02:01 +00002673 APInt API(Op1Int.getBitWidth(), 0);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002674 API.setBit(BitToSet);
Jay Foad15084f02010-11-30 09:02:01 +00002675 Op1 = ConstantInt::get(V->getContext(), API);
Victor Hernandez47444882009-11-10 08:28:35 +00002676 }
2677
Craig Topper9f008862014-04-15 04:59:12 +00002678 Value *Mul0 = nullptr;
Chris Lattner72d283c2010-09-05 17:20:46 +00002679 if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) {
2680 if (Constant *Op1C = dyn_cast<Constant>(Op1))
2681 if (Constant *MulC = dyn_cast<Constant>(Mul0)) {
Craig Topper1bef2c82012-12-22 19:15:35 +00002682 if (Op1C->getType()->getPrimitiveSizeInBits() <
Chris Lattner72d283c2010-09-05 17:20:46 +00002683 MulC->getType()->getPrimitiveSizeInBits())
2684 Op1C = ConstantExpr::getZExt(Op1C, MulC->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002685 if (Op1C->getType()->getPrimitiveSizeInBits() >
Chris Lattner72d283c2010-09-05 17:20:46 +00002686 MulC->getType()->getPrimitiveSizeInBits())
2687 MulC = ConstantExpr::getZExt(MulC, Op1C->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002688
Chris Lattner72d283c2010-09-05 17:20:46 +00002689 // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
2690 Multiple = ConstantExpr::getMul(MulC, Op1C);
2691 return true;
2692 }
Victor Hernandez47444882009-11-10 08:28:35 +00002693
2694 if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
2695 if (Mul0CI->getValue() == 1) {
2696 // V == Base * Op1, so return Op1
2697 Multiple = Op1;
2698 return true;
2699 }
2700 }
2701
Craig Topper9f008862014-04-15 04:59:12 +00002702 Value *Mul1 = nullptr;
Chris Lattner72d283c2010-09-05 17:20:46 +00002703 if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) {
2704 if (Constant *Op0C = dyn_cast<Constant>(Op0))
2705 if (Constant *MulC = dyn_cast<Constant>(Mul1)) {
Craig Topper1bef2c82012-12-22 19:15:35 +00002706 if (Op0C->getType()->getPrimitiveSizeInBits() <
Chris Lattner72d283c2010-09-05 17:20:46 +00002707 MulC->getType()->getPrimitiveSizeInBits())
2708 Op0C = ConstantExpr::getZExt(Op0C, MulC->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002709 if (Op0C->getType()->getPrimitiveSizeInBits() >
Chris Lattner72d283c2010-09-05 17:20:46 +00002710 MulC->getType()->getPrimitiveSizeInBits())
2711 MulC = ConstantExpr::getZExt(MulC, Op0C->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002712
Chris Lattner72d283c2010-09-05 17:20:46 +00002713 // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
2714 Multiple = ConstantExpr::getMul(MulC, Op0C);
2715 return true;
2716 }
Victor Hernandez47444882009-11-10 08:28:35 +00002717
2718 if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
2719 if (Mul1CI->getValue() == 1) {
2720 // V == Base * Op0, so return Op0
2721 Multiple = Op0;
2722 return true;
2723 }
2724 }
Victor Hernandez47444882009-11-10 08:28:35 +00002725 }
2726 }
2727
2728 // We could not determine if V is a multiple of Base.
2729 return false;
2730}
2731
David Majnemerb4b27232016-04-19 19:10:21 +00002732Intrinsic::ID llvm::getIntrinsicForCallSite(ImmutableCallSite ICS,
2733 const TargetLibraryInfo *TLI) {
2734 const Function *F = ICS.getCalledFunction();
2735 if (!F)
2736 return Intrinsic::not_intrinsic;
2737
2738 if (F->isIntrinsic())
2739 return F->getIntrinsicID();
2740
2741 if (!TLI)
2742 return Intrinsic::not_intrinsic;
2743
David L. Jonesd21529f2017-01-23 23:16:46 +00002744 LibFunc Func;
David Majnemerb4b27232016-04-19 19:10:21 +00002745 // We're going to make assumptions on the semantics of the functions, check
2746 // that the target knows that it's available in this environment and it does
2747 // not have local linkage.
Ahmed Bougachad765a822016-04-27 19:04:35 +00002748 if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(*F, Func))
2749 return Intrinsic::not_intrinsic;
2750
2751 if (!ICS.onlyReadsMemory())
David Majnemerb4b27232016-04-19 19:10:21 +00002752 return Intrinsic::not_intrinsic;
2753
2754 // Otherwise check if we have a call to a function that can be turned into a
2755 // vector intrinsic.
2756 switch (Func) {
2757 default:
2758 break;
David L. Jonesd21529f2017-01-23 23:16:46 +00002759 case LibFunc_sin:
2760 case LibFunc_sinf:
2761 case LibFunc_sinl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002762 return Intrinsic::sin;
David L. Jonesd21529f2017-01-23 23:16:46 +00002763 case LibFunc_cos:
2764 case LibFunc_cosf:
2765 case LibFunc_cosl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002766 return Intrinsic::cos;
David L. Jonesd21529f2017-01-23 23:16:46 +00002767 case LibFunc_exp:
2768 case LibFunc_expf:
2769 case LibFunc_expl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002770 return Intrinsic::exp;
David L. Jonesd21529f2017-01-23 23:16:46 +00002771 case LibFunc_exp2:
2772 case LibFunc_exp2f:
2773 case LibFunc_exp2l:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002774 return Intrinsic::exp2;
David L. Jonesd21529f2017-01-23 23:16:46 +00002775 case LibFunc_log:
2776 case LibFunc_logf:
2777 case LibFunc_logl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002778 return Intrinsic::log;
David L. Jonesd21529f2017-01-23 23:16:46 +00002779 case LibFunc_log10:
2780 case LibFunc_log10f:
2781 case LibFunc_log10l:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002782 return Intrinsic::log10;
David L. Jonesd21529f2017-01-23 23:16:46 +00002783 case LibFunc_log2:
2784 case LibFunc_log2f:
2785 case LibFunc_log2l:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002786 return Intrinsic::log2;
David L. Jonesd21529f2017-01-23 23:16:46 +00002787 case LibFunc_fabs:
2788 case LibFunc_fabsf:
2789 case LibFunc_fabsl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002790 return Intrinsic::fabs;
David L. Jonesd21529f2017-01-23 23:16:46 +00002791 case LibFunc_fmin:
2792 case LibFunc_fminf:
2793 case LibFunc_fminl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002794 return Intrinsic::minnum;
David L. Jonesd21529f2017-01-23 23:16:46 +00002795 case LibFunc_fmax:
2796 case LibFunc_fmaxf:
2797 case LibFunc_fmaxl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002798 return Intrinsic::maxnum;
David L. Jonesd21529f2017-01-23 23:16:46 +00002799 case LibFunc_copysign:
2800 case LibFunc_copysignf:
2801 case LibFunc_copysignl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002802 return Intrinsic::copysign;
David L. Jonesd21529f2017-01-23 23:16:46 +00002803 case LibFunc_floor:
2804 case LibFunc_floorf:
2805 case LibFunc_floorl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002806 return Intrinsic::floor;
David L. Jonesd21529f2017-01-23 23:16:46 +00002807 case LibFunc_ceil:
2808 case LibFunc_ceilf:
2809 case LibFunc_ceill:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002810 return Intrinsic::ceil;
David L. Jonesd21529f2017-01-23 23:16:46 +00002811 case LibFunc_trunc:
2812 case LibFunc_truncf:
2813 case LibFunc_truncl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002814 return Intrinsic::trunc;
David L. Jonesd21529f2017-01-23 23:16:46 +00002815 case LibFunc_rint:
2816 case LibFunc_rintf:
2817 case LibFunc_rintl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002818 return Intrinsic::rint;
David L. Jonesd21529f2017-01-23 23:16:46 +00002819 case LibFunc_nearbyint:
2820 case LibFunc_nearbyintf:
2821 case LibFunc_nearbyintl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002822 return Intrinsic::nearbyint;
David L. Jonesd21529f2017-01-23 23:16:46 +00002823 case LibFunc_round:
2824 case LibFunc_roundf:
2825 case LibFunc_roundl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002826 return Intrinsic::round;
David L. Jonesd21529f2017-01-23 23:16:46 +00002827 case LibFunc_pow:
2828 case LibFunc_powf:
2829 case LibFunc_powl:
Ahmed Bougachad765a822016-04-27 19:04:35 +00002830 return Intrinsic::pow;
David L. Jonesd21529f2017-01-23 23:16:46 +00002831 case LibFunc_sqrt:
2832 case LibFunc_sqrtf:
2833 case LibFunc_sqrtl:
Sanjay Patel86d24f12017-11-06 22:40:09 +00002834 return Intrinsic::sqrt;
David Majnemerb4b27232016-04-19 19:10:21 +00002835 }
2836
2837 return Intrinsic::not_intrinsic;
2838}
2839
Sanjay Patelaee84212014-11-04 16:27:42 +00002840/// Return true if we can prove that the specified FP value is never equal to
2841/// -0.0.
Chris Lattnera12a6de2008-06-02 01:29:46 +00002842///
2843/// NOTE: this function will need to be revisited when we support non-default
2844/// rounding modes!
David Majnemer3ee5f342016-04-13 06:55:52 +00002845bool llvm::CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI,
2846 unsigned Depth) {
Sanjay Patel20df88a2017-11-13 17:56:23 +00002847 if (auto *CFP = dyn_cast<ConstantFP>(V))
Chris Lattnera12a6de2008-06-02 01:29:46 +00002848 return !CFP->getValueAPF().isNegZero();
Craig Topper1bef2c82012-12-22 19:15:35 +00002849
Sanjay Patel20df88a2017-11-13 17:56:23 +00002850 // Limit search depth.
Matt Arsenaultcb2a7eb2016-12-20 19:06:15 +00002851 if (Depth == MaxDepth)
Sanjay Patel20df88a2017-11-13 17:56:23 +00002852 return false;
Chris Lattnera12a6de2008-06-02 01:29:46 +00002853
Sanjay Patel20df88a2017-11-13 17:56:23 +00002854 auto *Op = dyn_cast<Operator>(V);
2855 if (!Op)
2856 return false;
Michael Ilseman0f128372012-12-06 00:07:09 +00002857
Sanjay Patel20df88a2017-11-13 17:56:23 +00002858 // Check if the nsz fast-math flag is set.
2859 if (auto *FPO = dyn_cast<FPMathOperator>(Op))
Michael Ilseman0f128372012-12-06 00:07:09 +00002860 if (FPO->hasNoSignedZeros())
2861 return true;
2862
Sanjay Patel9e3d8f42017-11-13 17:40:47 +00002863 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
Sanjay Patel93e64dd2018-03-25 21:16:33 +00002864 if (match(Op, m_FAdd(m_Value(), m_PosZeroFP())))
Sanjay Patel9e3d8f42017-11-13 17:40:47 +00002865 return true;
Craig Topper1bef2c82012-12-22 19:15:35 +00002866
Chris Lattnera12a6de2008-06-02 01:29:46 +00002867 // sitofp and uitofp turn into +0.0 for zero.
Sanjay Patel20df88a2017-11-13 17:56:23 +00002868 if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op))
Chris Lattnera12a6de2008-06-02 01:29:46 +00002869 return true;
Craig Topper1bef2c82012-12-22 19:15:35 +00002870
Sanjay Patel20df88a2017-11-13 17:56:23 +00002871 if (auto *Call = dyn_cast<CallInst>(Op)) {
2872 Intrinsic::ID IID = getIntrinsicForCallSite(Call, TLI);
David Majnemer3ee5f342016-04-13 06:55:52 +00002873 switch (IID) {
2874 default:
2875 break;
Chris Lattnera12a6de2008-06-02 01:29:46 +00002876 // sqrt(-0.0) = -0.0, no other negative results are possible.
David Majnemer3ee5f342016-04-13 06:55:52 +00002877 case Intrinsic::sqrt:
Matt Arsenault56b31d82018-08-06 15:16:26 +00002878 case Intrinsic::canonicalize:
Sanjay Patel20df88a2017-11-13 17:56:23 +00002879 return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1);
David Majnemer3ee5f342016-04-13 06:55:52 +00002880 // fabs(x) != -0.0
2881 case Intrinsic::fabs:
2882 return true;
Chris Lattnera12a6de2008-06-02 01:29:46 +00002883 }
David Majnemer3ee5f342016-04-13 06:55:52 +00002884 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002885
Chris Lattnera12a6de2008-06-02 01:29:46 +00002886 return false;
2887}
2888
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002889/// If \p SignBitOnly is true, test for a known 0 sign bit rather than a
2890/// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign
2891/// bit despite comparing equal.
2892static bool cannotBeOrderedLessThanZeroImpl(const Value *V,
2893 const TargetLibraryInfo *TLI,
2894 bool SignBitOnly,
2895 unsigned Depth) {
Justin Lebar322c1272017-01-27 00:58:34 +00002896 // TODO: This function does not do the right thing when SignBitOnly is true
2897 // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform
2898 // which flips the sign bits of NaNs. See
2899 // https://llvm.org/bugs/show_bug.cgi?id=31702.
2900
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002901 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
2902 return !CFP->getValueAPF().isNegative() ||
2903 (!SignBitOnly && CFP->getValueAPF().isZero());
2904 }
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002905
Craig Topper69c89722018-02-26 22:33:17 +00002906 // Handle vector of constants.
2907 if (auto *CV = dyn_cast<Constant>(V)) {
2908 if (CV->getType()->isVectorTy()) {
2909 unsigned NumElts = CV->getType()->getVectorNumElements();
2910 for (unsigned i = 0; i != NumElts; ++i) {
2911 auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
2912 if (!CFP)
2913 return false;
2914 if (CFP->getValueAPF().isNegative() &&
2915 (SignBitOnly || !CFP->getValueAPF().isZero()))
2916 return false;
2917 }
2918
2919 // All non-negative ConstantFPs.
2920 return true;
2921 }
2922 }
2923
Matt Arsenaultcb2a7eb2016-12-20 19:06:15 +00002924 if (Depth == MaxDepth)
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002925 return false; // Limit search depth.
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002926
2927 const Operator *I = dyn_cast<Operator>(V);
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002928 if (!I)
2929 return false;
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002930
2931 switch (I->getOpcode()) {
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002932 default:
2933 break;
Fiona Glaserdb7824f2016-01-12 23:37:30 +00002934 // Unsigned integers are always nonnegative.
2935 case Instruction::UIToFP:
2936 return true;
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002937 case Instruction::FMul:
2938 // x*x is always non-negative or a NaN.
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002939 if (I->getOperand(0) == I->getOperand(1) &&
2940 (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()))
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002941 return true;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002942
Justin Bognercd1d5aa2016-08-17 20:30:52 +00002943 LLVM_FALLTHROUGH;
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002944 case Instruction::FAdd:
2945 case Instruction::FDiv:
2946 case Instruction::FRem:
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002947 return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
2948 Depth + 1) &&
2949 cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
2950 Depth + 1);
Fiona Glaserdb7824f2016-01-12 23:37:30 +00002951 case Instruction::Select:
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002952 return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
2953 Depth + 1) &&
2954 cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
2955 Depth + 1);
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002956 case Instruction::FPExt:
2957 case Instruction::FPTrunc:
2958 // Widening/narrowing never change sign.
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002959 return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
2960 Depth + 1);
Craig Topper30199102018-02-27 19:53:45 +00002961 case Instruction::ExtractElement:
2962 // Look through extract element. At the moment we keep this simple and skip
2963 // tracking the specific element. But at least we might find information
2964 // valid for all elements of the vector.
2965 return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
2966 Depth + 1);
David Majnemer3ee5f342016-04-13 06:55:52 +00002967 case Instruction::Call:
Justin Lebar7e3184c2017-01-26 00:10:26 +00002968 const auto *CI = cast<CallInst>(I);
2969 Intrinsic::ID IID = getIntrinsicForCallSite(CI, TLI);
David Majnemer3ee5f342016-04-13 06:55:52 +00002970 switch (IID) {
2971 default:
2972 break;
2973 case Intrinsic::maxnum:
Matt Arsenaultd54b7f02018-08-09 22:40:08 +00002974 return (isKnownNeverNaN(I->getOperand(0), TLI) &&
Sanjay Patelf9a0d592018-08-02 13:46:20 +00002975 cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI,
2976 SignBitOnly, Depth + 1)) ||
Matt Arsenaultd54b7f02018-08-09 22:40:08 +00002977 (isKnownNeverNaN(I->getOperand(1), TLI) &&
Sanjay Patelf9a0d592018-08-02 13:46:20 +00002978 cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI,
2979 SignBitOnly, Depth + 1));
2980
Thomas Livelyc3392502018-10-19 19:01:26 +00002981 case Intrinsic::maximum:
2982 return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
2983 Depth + 1) ||
2984 cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
2985 Depth + 1);
David Majnemer3ee5f342016-04-13 06:55:52 +00002986 case Intrinsic::minnum:
Thomas Livelyc3392502018-10-19 19:01:26 +00002987 case Intrinsic::minimum:
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00002988 return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
2989 Depth + 1) &&
2990 cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
2991 Depth + 1);
David Majnemer3ee5f342016-04-13 06:55:52 +00002992 case Intrinsic::exp:
2993 case Intrinsic::exp2:
2994 case Intrinsic::fabs:
David Majnemer3ee5f342016-04-13 06:55:52 +00002995 return true;
Justin Lebar7e3184c2017-01-26 00:10:26 +00002996
2997 case Intrinsic::sqrt:
2998 // sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0.
2999 if (!SignBitOnly)
3000 return true;
3001 return CI->hasNoNaNs() && (CI->hasNoSignedZeros() ||
3002 CannotBeNegativeZero(CI->getOperand(0), TLI));
3003
David Majnemer3ee5f342016-04-13 06:55:52 +00003004 case Intrinsic::powi:
Justin Lebar7e3184c2017-01-26 00:10:26 +00003005 if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand(1))) {
David Majnemer3ee5f342016-04-13 06:55:52 +00003006 // powi(x,n) is non-negative if n is even.
Justin Lebar7e3184c2017-01-26 00:10:26 +00003007 if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0)
David Majnemer3ee5f342016-04-13 06:55:52 +00003008 return true;
Elena Demikhovsky45f04482015-01-28 08:03:58 +00003009 }
Justin Lebar322c1272017-01-27 00:58:34 +00003010 // TODO: This is not correct. Given that exp is an integer, here are the
3011 // ways that pow can return a negative value:
3012 //
3013 // pow(x, exp) --> negative if exp is odd and x is negative.
3014 // pow(-0, exp) --> -inf if exp is negative odd.
3015 // pow(-0, exp) --> -0 if exp is positive odd.
3016 // pow(-inf, exp) --> -0 if exp is negative odd.
3017 // pow(-inf, exp) --> -inf if exp is positive odd.
3018 //
3019 // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN,
3020 // but we must return false if x == -0. Unfortunately we do not currently
3021 // have a way of expressing this constraint. See details in
3022 // https://llvm.org/bugs/show_bug.cgi?id=31702.
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00003023 return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
3024 Depth + 1);
Justin Lebar322c1272017-01-27 00:58:34 +00003025
David Majnemer3ee5f342016-04-13 06:55:52 +00003026 case Intrinsic::fma:
3027 case Intrinsic::fmuladd:
3028 // x*x+y is non-negative if y is non-negative.
3029 return I->getOperand(0) == I->getOperand(1) &&
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00003030 (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()) &&
3031 cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
3032 Depth + 1);
David Majnemer3ee5f342016-04-13 06:55:52 +00003033 }
Elena Demikhovsky45f04482015-01-28 08:03:58 +00003034 break;
3035 }
Sanjoy Das6082c1a2016-05-07 02:08:15 +00003036 return false;
Elena Demikhovsky45f04482015-01-28 08:03:58 +00003037}
3038
Matt Arsenault1e0edbf2017-01-11 00:33:24 +00003039bool llvm::CannotBeOrderedLessThanZero(const Value *V,
3040 const TargetLibraryInfo *TLI) {
3041 return cannotBeOrderedLessThanZeroImpl(V, TLI, false, 0);
3042}
3043
3044bool llvm::SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI) {
3045 return cannotBeOrderedLessThanZeroImpl(V, TLI, true, 0);
3046}
3047
Matt Arsenaultd54b7f02018-08-09 22:40:08 +00003048bool llvm::isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI,
3049 unsigned Depth) {
Sanjay Patel6840c5f2017-09-05 23:13:13 +00003050 assert(V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type");
3051
3052 // If we're told that NaNs won't happen, assume they won't.
3053 if (auto *FPMathOp = dyn_cast<FPMathOperator>(V))
3054 if (FPMathOp->hasNoNaNs())
3055 return true;
3056
Sanjay Patel6840c5f2017-09-05 23:13:13 +00003057 // Handle scalar constants.
3058 if (auto *CFP = dyn_cast<ConstantFP>(V))
3059 return !CFP->isNaN();
3060
Matt Arsenaultd54b7f02018-08-09 22:40:08 +00003061 if (Depth == MaxDepth)
3062 return false;
3063
Matt Arsenault450fcc72018-08-20 16:51:00 +00003064 if (auto *Inst = dyn_cast<Instruction>(V)) {
3065 switch (Inst->getOpcode()) {
3066 case Instruction::FAdd:
3067 case Instruction::FMul:
3068 case Instruction::FSub:
3069 case Instruction::FDiv:
3070 case Instruction::FRem: {
3071 // TODO: Need isKnownNeverInfinity
3072 return false;
3073 }
3074 case Instruction::Select: {
3075 return isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) &&
3076 isKnownNeverNaN(Inst->getOperand(2), TLI, Depth + 1);
3077 }
3078 case Instruction::SIToFP:
3079 case Instruction::UIToFP:
3080 return true;
3081 case Instruction::FPTrunc:
3082 case Instruction::FPExt:
3083 return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1);
3084 default:
3085 break;
3086 }
3087 }
3088
Matt Arsenaultd54b7f02018-08-09 22:40:08 +00003089 if (const auto *II = dyn_cast<IntrinsicInst>(V)) {
3090 switch (II->getIntrinsicID()) {
3091 case Intrinsic::canonicalize:
3092 case Intrinsic::fabs:
3093 case Intrinsic::copysign:
Matt Arsenault450fcc72018-08-20 16:51:00 +00003094 case Intrinsic::exp:
3095 case Intrinsic::exp2:
3096 case Intrinsic::floor:
3097 case Intrinsic::ceil:
3098 case Intrinsic::trunc:
3099 case Intrinsic::rint:
3100 case Intrinsic::nearbyint:
3101 case Intrinsic::round:
Matt Arsenaultd54b7f02018-08-09 22:40:08 +00003102 return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1);
3103 case Intrinsic::sqrt:
3104 return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) &&
3105 CannotBeOrderedLessThanZero(II->getArgOperand(0), TLI);
3106 default:
3107 return false;
3108 }
3109 }
3110
Sanjay Patel6840c5f2017-09-05 23:13:13 +00003111 // Bail out for constant expressions, but try to handle vector constants.
3112 if (!V->getType()->isVectorTy() || !isa<Constant>(V))
3113 return false;
3114
3115 // For vectors, verify that each element is not NaN.
3116 unsigned NumElts = V->getType()->getVectorNumElements();
3117 for (unsigned i = 0; i != NumElts; ++i) {
3118 Constant *Elt = cast<Constant>(V)->getAggregateElement(i);
3119 if (!Elt)
3120 return false;
3121 if (isa<UndefValue>(Elt))
3122 continue;
3123 auto *CElt = dyn_cast<ConstantFP>(Elt);
3124 if (!CElt || CElt->isNaN())
3125 return false;
3126 }
3127 // All elements were confirmed not-NaN or undefined.
3128 return true;
3129}
3130
Chris Lattner9cb10352010-12-26 20:15:01 +00003131Value *llvm::isBytewiseValue(Value *V) {
JF Bastien73d8e4e2018-09-21 05:17:42 +00003132
Chris Lattner9cb10352010-12-26 20:15:01 +00003133 // All byte-wide stores are splatable, even of arbitrary variables.
JF Bastien73d8e4e2018-09-21 05:17:42 +00003134 if (V->getType()->isIntegerTy(8))
3135 return V;
3136
3137 LLVMContext &Ctx = V->getContext();
3138
3139 // Undef don't care.
3140 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
3141 if (isa<UndefValue>(V))
3142 return UndefInt8;
3143
3144 Constant *C = dyn_cast<Constant>(V);
3145 if (!C) {
3146 // Conceptually, we could handle things like:
3147 // %a = zext i8 %X to i16
3148 // %b = shl i16 %a, 8
3149 // %c = or i16 %a, %b
3150 // but until there is an example that actually needs this, it doesn't seem
3151 // worth worrying about.
3152 return nullptr;
3153 }
Chris Lattneracf6b072011-02-19 19:35:49 +00003154
3155 // Handle 'null' ConstantArrayZero etc.
JF Bastien73d8e4e2018-09-21 05:17:42 +00003156 if (C->isNullValue())
3157 return Constant::getNullValue(Type::getInt8Ty(Ctx));
Craig Topper1bef2c82012-12-22 19:15:35 +00003158
JF Bastien73d8e4e2018-09-21 05:17:42 +00003159 // Constant floating-point values can be handled as integer values if the
Craig Topper1bef2c82012-12-22 19:15:35 +00003160 // corresponding integer value is "byteable". An important case is 0.0.
JF Bastien73d8e4e2018-09-21 05:17:42 +00003161 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3162 Type *Ty = nullptr;
3163 if (CFP->getType()->isHalfTy())
3164 Ty = Type::getInt16Ty(Ctx);
3165 else if (CFP->getType()->isFloatTy())
3166 Ty = Type::getInt32Ty(Ctx);
3167 else if (CFP->getType()->isDoubleTy())
3168 Ty = Type::getInt64Ty(Ctx);
Chris Lattner9cb10352010-12-26 20:15:01 +00003169 // Don't handle long double formats, which have strange constraints.
JF Bastien73d8e4e2018-09-21 05:17:42 +00003170 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty)) : nullptr;
Chris Lattner9cb10352010-12-26 20:15:01 +00003171 }
Craig Topper1bef2c82012-12-22 19:15:35 +00003172
Benjamin Kramer17d90152015-02-07 19:29:02 +00003173 // We can handle constant integers that are multiple of 8 bits.
JF Bastien73d8e4e2018-09-21 05:17:42 +00003174 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
Benjamin Kramer17d90152015-02-07 19:29:02 +00003175 if (CI->getBitWidth() % 8 == 0) {
3176 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
Benjamin Kramerb4b51502015-03-25 16:49:59 +00003177 if (!CI->getValue().isSplat(8))
Benjamin Kramer17d90152015-02-07 19:29:02 +00003178 return nullptr;
JF Bastien73d8e4e2018-09-21 05:17:42 +00003179 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
Chris Lattner9cb10352010-12-26 20:15:01 +00003180 }
3181 }
Craig Topper1bef2c82012-12-22 19:15:35 +00003182
JF Bastien73d8e4e2018-09-21 05:17:42 +00003183 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
3184 if (LHS == RHS)
3185 return LHS;
3186 if (!LHS || !RHS)
Craig Topper9f008862014-04-15 04:59:12 +00003187 return nullptr;
JF Bastien73d8e4e2018-09-21 05:17:42 +00003188 if (LHS == UndefInt8)
3189 return RHS;
3190 if (RHS == UndefInt8)
3191 return LHS;
3192 return nullptr;
3193 };
Craig Topper1bef2c82012-12-22 19:15:35 +00003194
JF Bastien73d8e4e2018-09-21 05:17:42 +00003195 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
3196 Value *Val = UndefInt8;
3197 for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
3198 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I)))))
Craig Topper9f008862014-04-15 04:59:12 +00003199 return nullptr;
Chris Lattner9cb10352010-12-26 20:15:01 +00003200 return Val;
3201 }
Chad Rosier8abf65a2011-12-06 00:19:08 +00003202
JF Bastien73d8e4e2018-09-21 05:17:42 +00003203 if (isa<ConstantVector>(C)) {
3204 Constant *Splat = cast<ConstantVector>(C)->getSplatValue();
3205 return Splat ? isBytewiseValue(Splat) : nullptr;
3206 }
3207
3208 if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) {
3209 Value *Val = UndefInt8;
3210 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
3211 if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I)))))
3212 return nullptr;
3213 return Val;
3214 }
3215
3216 // Don't try to handle the handful of other constants.
Craig Topper9f008862014-04-15 04:59:12 +00003217 return nullptr;
Chris Lattner9cb10352010-12-26 20:15:01 +00003218}
3219
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003220// This is the recursive version of BuildSubAggregate. It takes a few different
3221// arguments. Idxs is the index within the nested struct From that we are
3222// looking at now (which is of type IndexedType). IdxSkip is the number of
3223// indices from Idxs that should be left out when inserting into the resulting
3224// struct. To is the result struct built so far, new insertvalue instructions
3225// build on that.
Chris Lattner229907c2011-07-18 04:54:35 +00003226static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
Craig Topper2cd5ff82013-07-11 16:22:38 +00003227 SmallVectorImpl<unsigned> &Idxs,
Dan Gohmana6d0afc2009-08-07 01:32:21 +00003228 unsigned IdxSkip,
Dan Gohmana6d0afc2009-08-07 01:32:21 +00003229 Instruction *InsertBefore) {
Eugene Zelenko75075ef2017-09-01 21:37:29 +00003230 StructType *STy = dyn_cast<StructType>(IndexedType);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003231 if (STy) {
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003232 // Save the original To argument so we can modify it
3233 Value *OrigTo = To;
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003234 // General case, the type indexed by Idxs is a struct
3235 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3236 // Process each struct element recursively
3237 Idxs.push_back(i);
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003238 Value *PrevTo = To;
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00003239 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
Nick Lewycky39dbfd32009-11-23 03:29:18 +00003240 InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003241 Idxs.pop_back();
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003242 if (!To) {
3243 // Couldn't find any inserted value for this index? Cleanup
3244 while (PrevTo != OrigTo) {
3245 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
3246 PrevTo = Del->getAggregateOperand();
3247 Del->eraseFromParent();
3248 }
3249 // Stop processing elements
3250 break;
3251 }
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003252 }
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003253 // If we successfully found a value for each of our subaggregates
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003254 if (To)
3255 return To;
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003256 }
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003257 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
3258 // the struct's elements had a value that was inserted directly. In the latter
3259 // case, perhaps we can't determine each of the subelements individually, but
3260 // we might be able to find the complete struct somewhere.
Craig Topper1bef2c82012-12-22 19:15:35 +00003261
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003262 // Find the value that is at that particular spot
Jay Foad57aa6362011-07-13 10:26:04 +00003263 Value *V = FindInsertedValue(From, Idxs);
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003264
3265 if (!V)
Craig Topper9f008862014-04-15 04:59:12 +00003266 return nullptr;
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003267
Vedant Kumard3196742018-02-28 19:08:52 +00003268 // Insert the value in the new (sub) aggregate
Eugene Zelenko75075ef2017-09-01 21:37:29 +00003269 return InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip),
3270 "tmp", InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003271}
3272
3273// This helper takes a nested struct and extracts a part of it (which is again a
3274// struct) into a new value. For example, given the struct:
3275// { a, { b, { c, d }, e } }
3276// and the indices "1, 1" this returns
3277// { c, d }.
3278//
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003279// It does this by inserting an insertvalue for each element in the resulting
3280// struct, as opposed to just inserting a single struct. This will only work if
3281// each of the elements of the substruct are known (ie, inserted into From by an
3282// insertvalue instruction somewhere).
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003283//
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003284// All inserted insertvalue instructions are inserted before InsertBefore
Jay Foad57aa6362011-07-13 10:26:04 +00003285static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
Dan Gohmana6d0afc2009-08-07 01:32:21 +00003286 Instruction *InsertBefore) {
Matthijs Kooijman69801d42008-06-16 13:28:31 +00003287 assert(InsertBefore && "Must have someplace to insert!");
Chris Lattner229907c2011-07-18 04:54:35 +00003288 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
Jay Foad57aa6362011-07-13 10:26:04 +00003289 idx_range);
Owen Andersonb292b8c2009-07-30 23:03:37 +00003290 Value *To = UndefValue::get(IndexedType);
Jay Foad57aa6362011-07-13 10:26:04 +00003291 SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003292 unsigned IdxSkip = Idxs.size();
3293
Nick Lewycky39dbfd32009-11-23 03:29:18 +00003294 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003295}
3296
Vedant Kumard3196742018-02-28 19:08:52 +00003297/// Given an aggregate and a sequence of indices, see if the scalar value
3298/// indexed is already around as a register, for example if it was inserted
3299/// directly into the aggregate.
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00003300///
3301/// If InsertBefore is not null, this function will duplicate (modified)
3302/// insertvalues when a part of a nested struct is extracted.
Jay Foad57aa6362011-07-13 10:26:04 +00003303Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
3304 Instruction *InsertBefore) {
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003305 // Nothing to index? Just return V then (this is useful at the end of our
Chris Lattnerf7eb5432012-01-24 07:54:10 +00003306 // recursion).
Jay Foad57aa6362011-07-13 10:26:04 +00003307 if (idx_range.empty())
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003308 return V;
Chris Lattnerf7eb5432012-01-24 07:54:10 +00003309 // We have indices, so V should have an indexable type.
3310 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
3311 "Not looking at a struct or array?");
3312 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
3313 "Invalid indices for type?");
Owen Andersonf1f17432009-07-06 22:37:39 +00003314
Chris Lattner67058832012-01-25 06:48:06 +00003315 if (Constant *C = dyn_cast<Constant>(V)) {
3316 C = C->getAggregateElement(idx_range[0]);
Craig Topper9f008862014-04-15 04:59:12 +00003317 if (!C) return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +00003318 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
3319 }
Craig Topper1bef2c82012-12-22 19:15:35 +00003320
Chris Lattnerf7eb5432012-01-24 07:54:10 +00003321 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003322 // Loop the indices for the insertvalue instruction in parallel with the
3323 // requested indices
Jay Foad57aa6362011-07-13 10:26:04 +00003324 const unsigned *req_idx = idx_range.begin();
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00003325 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
3326 i != e; ++i, ++req_idx) {
Jay Foad57aa6362011-07-13 10:26:04 +00003327 if (req_idx == idx_range.end()) {
Chris Lattnerf7eb5432012-01-24 07:54:10 +00003328 // We can't handle this without inserting insertvalues
3329 if (!InsertBefore)
Craig Topper9f008862014-04-15 04:59:12 +00003330 return nullptr;
Chris Lattnerf7eb5432012-01-24 07:54:10 +00003331
3332 // The requested index identifies a part of a nested aggregate. Handle
3333 // this specially. For example,
3334 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
3335 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
3336 // %C = extractvalue {i32, { i32, i32 } } %B, 1
3337 // This can be changed into
3338 // %A = insertvalue {i32, i32 } undef, i32 10, 0
3339 // %C = insertvalue {i32, i32 } %A, i32 11, 1
3340 // which allows the unused 0,0 element from the nested struct to be
3341 // removed.
3342 return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
3343 InsertBefore);
Duncan Sandsdb356ee2008-06-19 08:47:31 +00003344 }
Craig Topper1bef2c82012-12-22 19:15:35 +00003345
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003346 // This insert value inserts something else than what we are looking for.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003347 // See if the (aggregate) value inserted into has the value we are
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003348 // looking for, then.
3349 if (*req_idx != *i)
Jay Foad57aa6362011-07-13 10:26:04 +00003350 return FindInsertedValue(I->getAggregateOperand(), idx_range,
Nick Lewycky39dbfd32009-11-23 03:29:18 +00003351 InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003352 }
3353 // If we end up here, the indices of the insertvalue match with those
3354 // requested (though possibly only partially). Now we recursively look at
3355 // the inserted value, passing any remaining indices.
Jay Foad57aa6362011-07-13 10:26:04 +00003356 return FindInsertedValue(I->getInsertedValueOperand(),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00003357 makeArrayRef(req_idx, idx_range.end()),
Nick Lewycky39dbfd32009-11-23 03:29:18 +00003358 InsertBefore);
Chris Lattnerf7eb5432012-01-24 07:54:10 +00003359 }
Craig Topper1bef2c82012-12-22 19:15:35 +00003360
Chris Lattnerf7eb5432012-01-24 07:54:10 +00003361 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003362 // If we're extracting a value from an aggregate that was extracted from
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003363 // something else, we can extract from that something else directly instead.
3364 // However, we will need to chain I's indices with the requested indices.
Craig Topper1bef2c82012-12-22 19:15:35 +00003365
3366 // Calculate the number of indices required
Jay Foad57aa6362011-07-13 10:26:04 +00003367 unsigned size = I->getNumIndices() + idx_range.size();
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003368 // Allocate some space to put the new indices in
Matthijs Kooijman8369c672008-06-17 08:24:37 +00003369 SmallVector<unsigned, 5> Idxs;
3370 Idxs.reserve(size);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003371 // Add indices from the extract value instruction
Jay Foad57aa6362011-07-13 10:26:04 +00003372 Idxs.append(I->idx_begin(), I->idx_end());
Craig Topper1bef2c82012-12-22 19:15:35 +00003373
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003374 // Add requested indices
Jay Foad57aa6362011-07-13 10:26:04 +00003375 Idxs.append(idx_range.begin(), idx_range.end());
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003376
Craig Topper1bef2c82012-12-22 19:15:35 +00003377 assert(Idxs.size() == size
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00003378 && "Number of indices added not correct?");
Craig Topper1bef2c82012-12-22 19:15:35 +00003379
Jay Foad57aa6362011-07-13 10:26:04 +00003380 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003381 }
3382 // Otherwise, we don't know (such as, extracting from a function return value
3383 // or load instruction)
Craig Topper9f008862014-04-15 04:59:12 +00003384 return nullptr;
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00003385}
Evan Chengda3db112008-06-30 07:31:25 +00003386
Sanjay Patelaee84212014-11-04 16:27:42 +00003387/// Analyze the specified pointer to see if it can be expressed as a base
3388/// pointer plus a constant offset. Return the base and offset to the caller.
Chris Lattnere28618d2010-11-30 22:25:26 +00003389Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003390 const DataLayout &DL) {
Elena Demikhovsky945b7e52018-02-14 06:58:08 +00003391 unsigned BitWidth = DL.getIndexTypeSizeInBits(Ptr->getType());
Nuno Lopes368c4d02012-12-31 20:48:35 +00003392 APInt ByteOffset(BitWidth, 0);
Chandler Carruth76641272016-01-04 07:23:12 +00003393
3394 // We walk up the defs but use a visited set to handle unreachable code. In
3395 // that case, we stop after accumulating the cycle once (not that it
3396 // matters).
3397 SmallPtrSet<Value *, 16> Visited;
3398 while (Visited.insert(Ptr).second) {
Nuno Lopes368c4d02012-12-31 20:48:35 +00003399 if (Ptr->getType()->isVectorTy())
3400 break;
Craig Topper1bef2c82012-12-22 19:15:35 +00003401
Nuno Lopes368c4d02012-12-31 20:48:35 +00003402 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
Tom Stellard17eb3412016-10-07 14:23:29 +00003403 // If one of the values we have visited is an addrspacecast, then
3404 // the pointer type of this GEP may be different from the type
3405 // of the Ptr parameter which was passed to this function. This
3406 // means when we construct GEPOffset, we need to use the size
3407 // of GEP's pointer type rather than the size of the original
3408 // pointer type.
Elena Demikhovsky945b7e52018-02-14 06:58:08 +00003409 APInt GEPOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003410 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
3411 break;
Matt Arsenaultf55e5e72013-08-10 17:34:08 +00003412
Florian Hahn79024052019-01-04 14:53:22 +00003413 APInt OrigByteOffset(ByteOffset);
3414 ByteOffset += GEPOffset.sextOrTrunc(ByteOffset.getBitWidth());
3415 if (ByteOffset.getMinSignedBits() > 64) {
3416 // Stop traversal if the pointer offset wouldn't fit into int64_t
3417 // (this should be removed if Offset is updated to an APInt)
3418 ByteOffset = OrigByteOffset;
3419 break;
3420 }
Matt Arsenaultf55e5e72013-08-10 17:34:08 +00003421
Nuno Lopes368c4d02012-12-31 20:48:35 +00003422 Ptr = GEP->getPointerOperand();
Tom Stellard17eb3412016-10-07 14:23:29 +00003423 } else if (Operator::getOpcode(Ptr) == Instruction::BitCast ||
3424 Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) {
Nuno Lopes368c4d02012-12-31 20:48:35 +00003425 Ptr = cast<Operator>(Ptr)->getOperand(0);
3426 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) {
Sanjoy Das5ce32722016-04-08 00:48:30 +00003427 if (GA->isInterposable())
Nuno Lopes368c4d02012-12-31 20:48:35 +00003428 break;
3429 Ptr = GA->getAliasee();
Chris Lattnere28618d2010-11-30 22:25:26 +00003430 } else {
Nuno Lopes368c4d02012-12-31 20:48:35 +00003431 break;
Chris Lattnere28618d2010-11-30 22:25:26 +00003432 }
3433 }
Nuno Lopes368c4d02012-12-31 20:48:35 +00003434 Offset = ByteOffset.getSExtValue();
3435 return Ptr;
Chris Lattnere28618d2010-11-30 22:25:26 +00003436}
3437
Matthias Braun50ec0b52017-05-19 22:37:09 +00003438bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP,
3439 unsigned CharSize) {
David L Kreitzer752c1442016-04-13 14:31:06 +00003440 // Make sure the GEP has exactly three arguments.
3441 if (GEP->getNumOperands() != 3)
3442 return false;
3443
Matthias Braun50ec0b52017-05-19 22:37:09 +00003444 // Make sure the index-ee is a pointer to array of \p CharSize integers.
3445 // CharSize.
David L Kreitzer752c1442016-04-13 14:31:06 +00003446 ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
Matthias Braun50ec0b52017-05-19 22:37:09 +00003447 if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
David L Kreitzer752c1442016-04-13 14:31:06 +00003448 return false;
3449
3450 // Check to make sure that the first operand of the GEP is an integer and
3451 // has value 0 so that we are sure we're indexing into the initializer.
3452 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
3453 if (!FirstIdx || !FirstIdx->isZero())
3454 return false;
3455
3456 return true;
Sanjoy Das6082c1a2016-05-07 02:08:15 +00003457}
Chris Lattnere28618d2010-11-30 22:25:26 +00003458
Matthias Braun50ec0b52017-05-19 22:37:09 +00003459bool llvm::getConstantDataArrayInfo(const Value *V,
3460 ConstantDataArraySlice &Slice,
3461 unsigned ElementSize, uint64_t Offset) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003462 assert(V);
Evan Chengda3db112008-06-30 07:31:25 +00003463
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003464 // Look through bitcast instructions and geps.
3465 V = V->stripPointerCasts();
Craig Topper1bef2c82012-12-22 19:15:35 +00003466
Benjamin Kramer0248a3e2015-03-21 15:36:06 +00003467 // If the value is a GEP instruction or constant expression, treat it as an
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003468 // offset.
3469 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
David L Kreitzer752c1442016-04-13 14:31:06 +00003470 // The GEP operator should be based on a pointer to string constant, and is
3471 // indexing into the string constant.
Matthias Braun50ec0b52017-05-19 22:37:09 +00003472 if (!isGEPBasedOnPointerToString(GEP, ElementSize))
Bill Wendlingfa54bc22009-03-13 04:39:26 +00003473 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00003474
Evan Chengda3db112008-06-30 07:31:25 +00003475 // If the second index isn't a ConstantInt, then this is a variable index
3476 // into the array. If this occurs, we can't say anything meaningful about
3477 // the string.
3478 uint64_t StartIdx = 0;
Dan Gohman0b4df042010-04-14 22:20:45 +00003479 if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
Evan Chengda3db112008-06-30 07:31:25 +00003480 StartIdx = CI->getZExtValue();
Bill Wendlingfa54bc22009-03-13 04:39:26 +00003481 else
3482 return false;
Matthias Braun50ec0b52017-05-19 22:37:09 +00003483 return getConstantDataArrayInfo(GEP->getOperand(0), Slice, ElementSize,
3484 StartIdx + Offset);
Evan Chengda3db112008-06-30 07:31:25 +00003485 }
Nick Lewycky46209882011-10-20 00:34:35 +00003486
Evan Chengda3db112008-06-30 07:31:25 +00003487 // The GEP instruction, constant or instruction, must reference a global
3488 // variable that is a constant and is initialized. The referenced constant
3489 // initializer is the array that we'll use for optimization.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003490 const GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
Dan Gohman5d5bc6d2009-08-19 18:20:44 +00003491 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
Bill Wendlingfa54bc22009-03-13 04:39:26 +00003492 return false;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003493
Matthias Braun50ec0b52017-05-19 22:37:09 +00003494 const ConstantDataArray *Array;
3495 ArrayType *ArrayTy;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003496 if (GV->getInitializer()->isNullValue()) {
Matthias Braun50ec0b52017-05-19 22:37:09 +00003497 Type *GVTy = GV->getValueType();
3498 if ( (ArrayTy = dyn_cast<ArrayType>(GVTy)) ) {
Sanjay Patel2ad88f82017-06-12 22:34:37 +00003499 // A zeroinitializer for the array; there is no ConstantDataArray.
Matthias Braun50ec0b52017-05-19 22:37:09 +00003500 Array = nullptr;
3501 } else {
3502 const DataLayout &DL = GV->getParent()->getDataLayout();
3503 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy);
3504 uint64_t Length = SizeInBytes / (ElementSize / 8);
3505 if (Length <= Offset)
3506 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00003507
Matthias Braun50ec0b52017-05-19 22:37:09 +00003508 Slice.Array = nullptr;
3509 Slice.Offset = 0;
3510 Slice.Length = Length - Offset;
3511 return true;
3512 }
3513 } else {
3514 // This must be a ConstantDataArray.
3515 Array = dyn_cast<ConstantDataArray>(GV->getInitializer());
3516 if (!Array)
3517 return false;
3518 ArrayTy = Array->getType();
3519 }
3520 if (!ArrayTy->getElementType()->isIntegerTy(ElementSize))
Bill Wendlingfa54bc22009-03-13 04:39:26 +00003521 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00003522
Matthias Braun50ec0b52017-05-19 22:37:09 +00003523 uint64_t NumElts = ArrayTy->getArrayNumElements();
Bill Wendlingfa54bc22009-03-13 04:39:26 +00003524 if (Offset > NumElts)
3525 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00003526
Matthias Braun50ec0b52017-05-19 22:37:09 +00003527 Slice.Array = Array;
3528 Slice.Offset = Offset;
3529 Slice.Length = NumElts - Offset;
3530 return true;
3531}
3532
3533/// This function computes the length of a null-terminated C string pointed to
3534/// by V. If successful, it returns true and returns the string in Str.
3535/// If unsuccessful, it returns false.
3536bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
3537 uint64_t Offset, bool TrimAtNul) {
3538 ConstantDataArraySlice Slice;
3539 if (!getConstantDataArrayInfo(V, Slice, 8, Offset))
3540 return false;
3541
3542 if (Slice.Array == nullptr) {
3543 if (TrimAtNul) {
3544 Str = StringRef();
3545 return true;
3546 }
3547 if (Slice.Length == 1) {
3548 Str = StringRef("", 1);
3549 return true;
3550 }
Sanjay Patelfef83e82017-06-09 14:21:18 +00003551 // We cannot instantiate a StringRef as we do not have an appropriate string
Matthias Braun50ec0b52017-05-19 22:37:09 +00003552 // of 0s at hand.
3553 return false;
3554 }
3555
3556 // Start out with the entire array in the StringRef.
3557 Str = Slice.Array->getAsString();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003558 // Skip over 'offset' bytes.
Matthias Braun50ec0b52017-05-19 22:37:09 +00003559 Str = Str.substr(Slice.Offset);
Craig Topper1bef2c82012-12-22 19:15:35 +00003560
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003561 if (TrimAtNul) {
3562 // Trim off the \0 and anything after it. If the array is not nul
3563 // terminated, we just return the whole end of string. The client may know
3564 // some other way that the string is length-bound.
3565 Str = Str.substr(0, Str.find('\0'));
3566 }
Bill Wendlingfa54bc22009-03-13 04:39:26 +00003567 return true;
Evan Chengda3db112008-06-30 07:31:25 +00003568}
Eric Christopher4899cbc2010-03-05 06:58:57 +00003569
3570// These next two are very similar to the above, but also look through PHI
3571// nodes.
3572// TODO: See if we can integrate these two together.
3573
Sanjay Patelaee84212014-11-04 16:27:42 +00003574/// If we can compute the length of the string pointed to by
Eric Christopher4899cbc2010-03-05 06:58:57 +00003575/// the specified pointer, return 'len+1'. If we can't, return 0.
Pete Cooper35b00d52016-08-13 01:05:32 +00003576static uint64_t GetStringLengthH(const Value *V,
Matthias Braun50ec0b52017-05-19 22:37:09 +00003577 SmallPtrSetImpl<const PHINode*> &PHIs,
3578 unsigned CharSize) {
Eric Christopher4899cbc2010-03-05 06:58:57 +00003579 // Look through noop bitcast instructions.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003580 V = V->stripPointerCasts();
Eric Christopher4899cbc2010-03-05 06:58:57 +00003581
3582 // If this is a PHI node, there are two cases: either we have already seen it
3583 // or we haven't.
Pete Cooper35b00d52016-08-13 01:05:32 +00003584 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
David Blaikie70573dc2014-11-19 07:49:26 +00003585 if (!PHIs.insert(PN).second)
Eric Christopher4899cbc2010-03-05 06:58:57 +00003586 return ~0ULL; // already in the set.
3587
3588 // If it was new, see if all the input strings are the same length.
3589 uint64_t LenSoFar = ~0ULL;
Pete Cooper833f34d2015-05-12 20:05:31 +00003590 for (Value *IncValue : PN->incoming_values()) {
Matthias Braun50ec0b52017-05-19 22:37:09 +00003591 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
Eric Christopher4899cbc2010-03-05 06:58:57 +00003592 if (Len == 0) return 0; // Unknown length -> unknown.
3593
3594 if (Len == ~0ULL) continue;
3595
3596 if (Len != LenSoFar && LenSoFar != ~0ULL)
3597 return 0; // Disagree -> unknown.
3598 LenSoFar = Len;
3599 }
3600
3601 // Success, all agree.
3602 return LenSoFar;
3603 }
3604
3605 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
Pete Cooper35b00d52016-08-13 01:05:32 +00003606 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
Matthias Braun50ec0b52017-05-19 22:37:09 +00003607 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
Eric Christopher4899cbc2010-03-05 06:58:57 +00003608 if (Len1 == 0) return 0;
Matthias Braun50ec0b52017-05-19 22:37:09 +00003609 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
Eric Christopher4899cbc2010-03-05 06:58:57 +00003610 if (Len2 == 0) return 0;
3611 if (Len1 == ~0ULL) return Len2;
3612 if (Len2 == ~0ULL) return Len1;
3613 if (Len1 != Len2) return 0;
3614 return Len1;
3615 }
Craig Topper1bef2c82012-12-22 19:15:35 +00003616
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003617 // Otherwise, see if we can read the string.
Matthias Braun50ec0b52017-05-19 22:37:09 +00003618 ConstantDataArraySlice Slice;
3619 if (!getConstantDataArrayInfo(V, Slice, CharSize))
Eric Christopher4899cbc2010-03-05 06:58:57 +00003620 return 0;
3621
Matthias Braun50ec0b52017-05-19 22:37:09 +00003622 if (Slice.Array == nullptr)
3623 return 1;
3624
3625 // Search for nul characters
3626 unsigned NullIndex = 0;
3627 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
3628 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
3629 break;
3630 }
3631
3632 return NullIndex + 1;
Eric Christopher4899cbc2010-03-05 06:58:57 +00003633}
3634
Sanjay Patelaee84212014-11-04 16:27:42 +00003635/// If we can compute the length of the string pointed to by
Eric Christopher4899cbc2010-03-05 06:58:57 +00003636/// the specified pointer, return 'len+1'. If we can't, return 0.
David Bolvansky1f343fa2018-05-22 20:27:36 +00003637uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
David Bolvansky41f4b642018-05-22 15:41:23 +00003638 if (!V->getType()->isPointerTy())
3639 return 0;
Eric Christopher4899cbc2010-03-05 06:58:57 +00003640
Pete Cooper35b00d52016-08-13 01:05:32 +00003641 SmallPtrSet<const PHINode*, 32> PHIs;
Matthias Braun50ec0b52017-05-19 22:37:09 +00003642 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
Eric Christopher4899cbc2010-03-05 06:58:57 +00003643 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
3644 // an empty string as a length.
3645 return Len == ~0ULL ? 1 : Len;
3646}
Dan Gohmana4fcd242010-12-15 20:02:24 +00003647
Chandler Carruth363ac682019-01-07 05:42:51 +00003648const Value *llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call) {
3649 assert(Call &&
3650 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
3651 if (const Value *RV = Call->getReturnedArgOperand())
Piotr Padlewskid6f73462018-05-23 09:16:44 +00003652 return RV;
3653 // This can be used only as a aliasing property.
Chandler Carruth363ac682019-01-07 05:42:51 +00003654 if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call))
3655 return Call->getArgOperand(0);
Piotr Padlewskid6f73462018-05-23 09:16:44 +00003656 return nullptr;
3657}
3658
3659bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
Chandler Carruth363ac682019-01-07 05:42:51 +00003660 const CallBase *Call) {
3661 return Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
3662 Call->getIntrinsicID() == Intrinsic::strip_invariant_group;
Piotr Padlewskid6f73462018-05-23 09:16:44 +00003663}
3664
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00003665/// \p PN defines a loop-variant pointer to an object. Check if the
Adam Nemete2b885c2015-04-23 20:09:20 +00003666/// previous iteration of the loop was referring to the same object as \p PN.
Pete Cooper35b00d52016-08-13 01:05:32 +00003667static bool isSameUnderlyingObjectInLoop(const PHINode *PN,
3668 const LoopInfo *LI) {
Adam Nemete2b885c2015-04-23 20:09:20 +00003669 // Find the loop-defined value.
3670 Loop *L = LI->getLoopFor(PN->getParent());
3671 if (PN->getNumIncomingValues() != 2)
3672 return true;
3673
3674 // Find the value from previous iteration.
3675 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
3676 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
3677 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
3678 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
3679 return true;
3680
3681 // If a new pointer is loaded in the loop, the pointer references a different
3682 // object in every iteration. E.g.:
3683 // for (i)
3684 // int *p = a[i];
3685 // ...
3686 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
3687 if (!L->isLoopInvariant(Load->getPointerOperand()))
3688 return false;
3689 return true;
3690}
3691
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003692Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL,
3693 unsigned MaxLookup) {
Dan Gohmana4fcd242010-12-15 20:02:24 +00003694 if (!V->getType()->isPointerTy())
3695 return V;
3696 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
3697 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
3698 V = GEP->getPointerOperand();
Matt Arsenault70f4db882014-07-15 00:56:40 +00003699 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
3700 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
Dan Gohmana4fcd242010-12-15 20:02:24 +00003701 V = cast<Operator>(V)->getOperand(0);
3702 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
Sanjoy Das5ce32722016-04-08 00:48:30 +00003703 if (GA->isInterposable())
Dan Gohmana4fcd242010-12-15 20:02:24 +00003704 return V;
3705 V = GA->getAliasee();
Craig Topper85482412017-04-12 22:29:23 +00003706 } else if (isa<AllocaInst>(V)) {
3707 // An alloca can't be further simplified.
3708 return V;
Dan Gohmana4fcd242010-12-15 20:02:24 +00003709 } else {
Chandler Carruth363ac682019-01-07 05:42:51 +00003710 if (auto *Call = dyn_cast<CallBase>(V)) {
Piotr Padlewski5b3db452018-07-02 04:49:30 +00003711 // CaptureTracking can know about special capturing properties of some
3712 // intrinsics like launder.invariant.group, that can't be expressed with
3713 // the attributes, but have properties like returning aliasing pointer.
3714 // Because some analysis may assume that nocaptured pointer is not
3715 // returned from some special intrinsic (because function would have to
3716 // be marked with returns attribute), it is crucial to use this function
3717 // because it should be in sync with CaptureTracking. Not using it may
3718 // cause weird miscompilations where 2 aliasing pointers are assumed to
3719 // noalias.
Chandler Carruth363ac682019-01-07 05:42:51 +00003720 if (auto *RP = getArgumentAliasingToReturnedPointer(Call)) {
Piotr Padlewskid6f73462018-05-23 09:16:44 +00003721 V = RP;
Hal Finkel5c12d8f2016-07-11 01:32:20 +00003722 continue;
3723 }
Piotr Padlewskid6f73462018-05-23 09:16:44 +00003724 }
Hal Finkel5c12d8f2016-07-11 01:32:20 +00003725
Dan Gohman05b18f12010-12-15 20:49:55 +00003726 // See if InstructionSimplify knows any relevant tricks.
3727 if (Instruction *I = dyn_cast<Instruction>(V))
Daniel Jasperaec2fa32016-12-19 08:22:17 +00003728 // TODO: Acquire a DominatorTree and AssumptionCache and use them.
Daniel Berlin4d0fe642017-04-28 19:55:38 +00003729 if (Value *Simplified = SimplifyInstruction(I, {DL, I})) {
Dan Gohman05b18f12010-12-15 20:49:55 +00003730 V = Simplified;
3731 continue;
3732 }
3733
Dan Gohmana4fcd242010-12-15 20:02:24 +00003734 return V;
3735 }
3736 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
3737 }
3738 return V;
3739}
Nick Lewycky3e334a42011-06-27 04:20:45 +00003740
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003741void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
Adam Nemete2b885c2015-04-23 20:09:20 +00003742 const DataLayout &DL, LoopInfo *LI,
3743 unsigned MaxLookup) {
Dan Gohmaned7c24e22012-05-10 18:57:38 +00003744 SmallPtrSet<Value *, 4> Visited;
3745 SmallVector<Value *, 4> Worklist;
3746 Worklist.push_back(V);
3747 do {
3748 Value *P = Worklist.pop_back_val();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003749 P = GetUnderlyingObject(P, DL, MaxLookup);
Dan Gohmaned7c24e22012-05-10 18:57:38 +00003750
David Blaikie70573dc2014-11-19 07:49:26 +00003751 if (!Visited.insert(P).second)
Dan Gohmaned7c24e22012-05-10 18:57:38 +00003752 continue;
3753
3754 if (SelectInst *SI = dyn_cast<SelectInst>(P)) {
3755 Worklist.push_back(SI->getTrueValue());
3756 Worklist.push_back(SI->getFalseValue());
3757 continue;
3758 }
3759
3760 if (PHINode *PN = dyn_cast<PHINode>(P)) {
Adam Nemete2b885c2015-04-23 20:09:20 +00003761 // If this PHI changes the underlying object in every iteration of the
3762 // loop, don't look through it. Consider:
3763 // int **A;
3764 // for (i) {
3765 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
3766 // Curr = A[i];
3767 // *Prev, *Curr;
3768 //
3769 // Prev is tracking Curr one iteration behind so they refer to different
3770 // underlying objects.
3771 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
3772 isSameUnderlyingObjectInLoop(PN, LI))
Pete Cooper833f34d2015-05-12 20:05:31 +00003773 for (Value *IncValue : PN->incoming_values())
3774 Worklist.push_back(IncValue);
Dan Gohmaned7c24e22012-05-10 18:57:38 +00003775 continue;
3776 }
3777
3778 Objects.push_back(P);
3779 } while (!Worklist.empty());
3780}
3781
Hiroshi Inoueb9417db2017-08-01 03:32:15 +00003782/// This is the function that does the work of looking through basic
3783/// ptrtoint+arithmetic+inttoptr sequences.
3784static const Value *getUnderlyingObjectFromInt(const Value *V) {
3785 do {
3786 if (const Operator *U = dyn_cast<Operator>(V)) {
3787 // If we find a ptrtoint, we can transfer control back to the
3788 // regular getUnderlyingObjectFromInt.
3789 if (U->getOpcode() == Instruction::PtrToInt)
3790 return U->getOperand(0);
3791 // If we find an add of a constant, a multiplied value, or a phi, it's
3792 // likely that the other operand will lead us to the base
3793 // object. We don't have to worry about the case where the
3794 // object address is somehow being computed by the multiply,
3795 // because our callers only care when the result is an
3796 // identifiable object.
3797 if (U->getOpcode() != Instruction::Add ||
3798 (!isa<ConstantInt>(U->getOperand(1)) &&
3799 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
3800 !isa<PHINode>(U->getOperand(1))))
3801 return V;
3802 V = U->getOperand(0);
3803 } else {
3804 return V;
3805 }
3806 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
3807 } while (true);
3808}
3809
3810/// This is a wrapper around GetUnderlyingObjects and adds support for basic
3811/// ptrtoint+arithmetic+inttoptr sequences.
Hiroshi Inoueb49b0152017-10-12 06:26:04 +00003812/// It returns false if unidentified object is found in GetUnderlyingObjects.
3813bool llvm::getUnderlyingObjectsForCodeGen(const Value *V,
Hiroshi Inoueb9417db2017-08-01 03:32:15 +00003814 SmallVectorImpl<Value *> &Objects,
3815 const DataLayout &DL) {
3816 SmallPtrSet<const Value *, 16> Visited;
3817 SmallVector<const Value *, 4> Working(1, V);
3818 do {
3819 V = Working.pop_back_val();
3820
3821 SmallVector<Value *, 4> Objs;
3822 GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL);
3823
3824 for (Value *V : Objs) {
Hiroshi Inoueb9417db2017-08-01 03:32:15 +00003825 if (!Visited.insert(V).second)
3826 continue;
3827 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
3828 const Value *O =
3829 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
3830 if (O->getType()->isPointerTy()) {
3831 Working.push_back(O);
3832 continue;
3833 }
3834 }
Hiroshi Inoue0bd906e2017-08-02 18:16:32 +00003835 // If GetUnderlyingObjects fails to find an identifiable object,
3836 // getUnderlyingObjectsForCodeGen also fails for safety.
3837 if (!isIdentifiedObject(V)) {
3838 Objects.clear();
Hiroshi Inoueb49b0152017-10-12 06:26:04 +00003839 return false;
Hiroshi Inoue0bd906e2017-08-02 18:16:32 +00003840 }
Hiroshi Inoueb9417db2017-08-01 03:32:15 +00003841 Objects.push_back(const_cast<Value *>(V));
3842 }
3843 } while (!Working.empty());
Hiroshi Inoueb49b0152017-10-12 06:26:04 +00003844 return true;
Hiroshi Inoueb9417db2017-08-01 03:32:15 +00003845}
3846
Sanjay Patelaee84212014-11-04 16:27:42 +00003847/// Return true if the only users of this pointer are lifetime markers.
Nick Lewycky3e334a42011-06-27 04:20:45 +00003848bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00003849 for (const User *U : V->users()) {
3850 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
Nick Lewycky3e334a42011-06-27 04:20:45 +00003851 if (!II) return false;
3852
Vedant Kumarb264d692018-12-21 21:49:40 +00003853 if (!II->isLifetimeStartOrEnd())
Nick Lewycky3e334a42011-06-27 04:20:45 +00003854 return false;
3855 }
3856 return true;
3857}
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003858
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00003859bool llvm::isSafeToSpeculativelyExecute(const Value *V,
3860 const Instruction *CtxI,
Sean Silva45835e72016-07-02 23:47:27 +00003861 const DominatorTree *DT) {
Dan Gohman7ac046a2012-01-04 23:01:09 +00003862 const Operator *Inst = dyn_cast<Operator>(V);
3863 if (!Inst)
3864 return false;
3865
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003866 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
3867 if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i)))
3868 if (C->canTrap())
3869 return false;
3870
3871 switch (Inst->getOpcode()) {
3872 default:
3873 return true;
3874 case Instruction::UDiv:
David Majnemerf20d7c42014-11-04 23:49:08 +00003875 case Instruction::URem: {
3876 // x / y is undefined if y == 0.
3877 const APInt *V;
3878 if (match(Inst->getOperand(1), m_APInt(V)))
3879 return *V != 0;
3880 return false;
3881 }
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003882 case Instruction::SDiv:
3883 case Instruction::SRem: {
David Majnemerf20d7c42014-11-04 23:49:08 +00003884 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
David Majnemer8a6578a2015-02-01 19:10:19 +00003885 const APInt *Numerator, *Denominator;
3886 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
3887 return false;
3888 // We cannot hoist this division if the denominator is 0.
3889 if (*Denominator == 0)
3890 return false;
3891 // It's safe to hoist if the denominator is not 0 or -1.
3892 if (*Denominator != -1)
3893 return true;
3894 // At this point we know that the denominator is -1. It is safe to hoist as
3895 // long we know that the numerator is not INT_MIN.
3896 if (match(Inst->getOperand(0), m_APInt(Numerator)))
3897 return !Numerator->isMinSignedValue();
3898 // The numerator *might* be MinSignedValue.
David Majnemerf20d7c42014-11-04 23:49:08 +00003899 return false;
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003900 }
3901 case Instruction::Load: {
3902 const LoadInst *LI = cast<LoadInst>(Inst);
Kostya Serebryany0b458282013-11-21 07:29:28 +00003903 if (!LI->isUnordered() ||
3904 // Speculative load may create a race that did not exist in the source.
Sanjoy Dasb66374c2016-07-14 20:19:01 +00003905 LI->getFunction()->hasFnAttribute(Attribute::SanitizeThread) ||
Kostya Serebryany5cb86d52015-10-14 00:21:05 +00003906 // Speculative load may load data from dirty regions.
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +00003907 LI->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
3908 LI->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress))
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003909 return false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003910 const DataLayout &DL = LI->getModule()->getDataLayout();
Sean Silva45835e72016-07-02 23:47:27 +00003911 return isDereferenceableAndAlignedPointer(LI->getPointerOperand(),
3912 LI->getAlignment(), DL, CtxI, DT);
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003913 }
Nick Lewyckyb4039f62011-12-21 05:52:02 +00003914 case Instruction::Call: {
Matt Arsenaultcf5e7fe2017-04-28 21:13:09 +00003915 auto *CI = cast<const CallInst>(Inst);
3916 const Function *Callee = CI->getCalledFunction();
David Majnemer0a92f862015-08-28 21:13:39 +00003917
Matt Arsenault6a288c12017-05-03 02:26:10 +00003918 // The called function could have undefined behavior or side-effects, even
3919 // if marked readnone nounwind.
3920 return Callee && Callee->isSpeculatable();
Nick Lewyckyb4039f62011-12-21 05:52:02 +00003921 }
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003922 case Instruction::VAArg:
3923 case Instruction::Alloca:
3924 case Instruction::Invoke:
3925 case Instruction::PHI:
3926 case Instruction::Store:
3927 case Instruction::Ret:
3928 case Instruction::Br:
3929 case Instruction::IndirectBr:
3930 case Instruction::Switch:
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003931 case Instruction::Unreachable:
3932 case Instruction::Fence:
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003933 case Instruction::AtomicRMW:
3934 case Instruction::AtomicCmpXchg:
David Majnemer654e1302015-07-31 17:58:14 +00003935 case Instruction::LandingPad:
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003936 case Instruction::Resume:
David Majnemer8a1c45d2015-12-12 05:38:55 +00003937 case Instruction::CatchSwitch:
David Majnemer654e1302015-07-31 17:58:14 +00003938 case Instruction::CatchPad:
David Majnemer654e1302015-07-31 17:58:14 +00003939 case Instruction::CatchRet:
3940 case Instruction::CleanupPad:
3941 case Instruction::CleanupRet:
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003942 return false; // Misc instructions which have effects
3943 }
3944}
Dan Gohman1b0f79d2013-01-31 02:40:59 +00003945
Quentin Colombet6443cce2015-08-06 18:44:34 +00003946bool llvm::mayBeMemoryDependent(const Instruction &I) {
3947 return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I);
3948}
3949
Florian Hahn19f9e322018-08-17 14:39:04 +00003950OverflowResult llvm::computeOverflowForUnsignedMul(
3951 const Value *LHS, const Value *RHS, const DataLayout &DL,
3952 AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
3953 bool UseInstrInfo) {
David Majnemer491331a2015-01-02 07:29:43 +00003954 // Multiplying n * m significant bits yields a result of n + m significant
3955 // bits. If the total number of significant bits does not exceed the
3956 // result bit width (minus 1), there is no overflow.
3957 // This means if we have enough leading zero bits in the operands
3958 // we can guarantee that the result does not overflow.
3959 // Ref: "Hacker's Delight" by Henry Warren
3960 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
Craig Topperb45eabc2017-04-26 16:39:58 +00003961 KnownBits LHSKnown(BitWidth);
3962 KnownBits RHSKnown(BitWidth);
Florian Hahn19f9e322018-08-17 14:39:04 +00003963 computeKnownBits(LHS, LHSKnown, DL, /*Depth=*/0, AC, CxtI, DT, nullptr,
3964 UseInstrInfo);
3965 computeKnownBits(RHS, RHSKnown, DL, /*Depth=*/0, AC, CxtI, DT, nullptr,
3966 UseInstrInfo);
David Majnemer491331a2015-01-02 07:29:43 +00003967 // Note that underestimating the number of zero bits gives a more
3968 // conservative answer.
Craig Topper8df66c62017-05-12 17:20:30 +00003969 unsigned ZeroBits = LHSKnown.countMinLeadingZeros() +
3970 RHSKnown.countMinLeadingZeros();
David Majnemer491331a2015-01-02 07:29:43 +00003971 // First handle the easy case: if we have enough zero bits there's
3972 // definitely no overflow.
3973 if (ZeroBits >= BitWidth)
3974 return OverflowResult::NeverOverflows;
3975
3976 // Get the largest possible values for each operand.
Craig Topperb45eabc2017-04-26 16:39:58 +00003977 APInt LHSMax = ~LHSKnown.Zero;
3978 APInt RHSMax = ~RHSKnown.Zero;
David Majnemer491331a2015-01-02 07:29:43 +00003979
3980 // We know the multiply operation doesn't overflow if the maximum values for
3981 // each operand will not overflow after we multiply them together.
David Majnemerc8a576b2015-01-02 07:29:47 +00003982 bool MaxOverflow;
Craig Topper9b71a402017-04-19 21:09:45 +00003983 (void)LHSMax.umul_ov(RHSMax, MaxOverflow);
David Majnemerc8a576b2015-01-02 07:29:47 +00003984 if (!MaxOverflow)
3985 return OverflowResult::NeverOverflows;
David Majnemer491331a2015-01-02 07:29:43 +00003986
David Majnemerc8a576b2015-01-02 07:29:47 +00003987 // We know it always overflows if multiplying the smallest possible values for
3988 // the operands also results in overflow.
3989 bool MinOverflow;
Craig Topperb45eabc2017-04-26 16:39:58 +00003990 (void)LHSKnown.One.umul_ov(RHSKnown.One, MinOverflow);
David Majnemerc8a576b2015-01-02 07:29:47 +00003991 if (MinOverflow)
3992 return OverflowResult::AlwaysOverflows;
3993
3994 return OverflowResult::MayOverflow;
David Majnemer491331a2015-01-02 07:29:43 +00003995}
David Majnemer5310c1e2015-01-07 00:39:50 +00003996
Florian Hahn19f9e322018-08-17 14:39:04 +00003997OverflowResult
3998llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
3999 const DataLayout &DL, AssumptionCache *AC,
4000 const Instruction *CxtI,
4001 const DominatorTree *DT, bool UseInstrInfo) {
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +00004002 // Multiplying n * m significant bits yields a result of n + m significant
4003 // bits. If the total number of significant bits does not exceed the
4004 // result bit width (minus 1), there is no overflow.
4005 // This means if we have enough leading sign bits in the operands
4006 // we can guarantee that the result does not overflow.
4007 // Ref: "Hacker's Delight" by Henry Warren
4008 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
4009
4010 // Note that underestimating the number of sign bits gives a more
4011 // conservative answer.
4012 unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) +
4013 ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT);
4014
4015 // First handle the easy case: if we have enough sign bits there's
4016 // definitely no overflow.
4017 if (SignBits > BitWidth + 1)
4018 return OverflowResult::NeverOverflows;
4019
4020 // There are two ambiguous cases where there can be no overflow:
4021 // SignBits == BitWidth + 1 and
4022 // SignBits == BitWidth
4023 // The second case is difficult to check, therefore we only handle the
4024 // first case.
4025 if (SignBits == BitWidth + 1) {
4026 // It overflows only when both arguments are negative and the true
4027 // product is exactly the minimum negative number.
4028 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
4029 // For simplicity we just check if at least one side is not negative.
Florian Hahn19f9e322018-08-17 14:39:04 +00004030 KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
4031 nullptr, UseInstrInfo);
4032 KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
4033 nullptr, UseInstrInfo);
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +00004034 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
4035 return OverflowResult::NeverOverflows;
4036 }
4037 return OverflowResult::MayOverflow;
4038}
4039
Florian Hahn19f9e322018-08-17 14:39:04 +00004040OverflowResult llvm::computeOverflowForUnsignedAdd(
4041 const Value *LHS, const Value *RHS, const DataLayout &DL,
4042 AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
4043 bool UseInstrInfo) {
4044 KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
4045 nullptr, UseInstrInfo);
Craig Topper6e11a052017-05-08 16:22:48 +00004046 if (LHSKnown.isNonNegative() || LHSKnown.isNegative()) {
Florian Hahn19f9e322018-08-17 14:39:04 +00004047 KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
4048 nullptr, UseInstrInfo);
David Majnemer5310c1e2015-01-07 00:39:50 +00004049
Craig Topper6e11a052017-05-08 16:22:48 +00004050 if (LHSKnown.isNegative() && RHSKnown.isNegative()) {
David Majnemer5310c1e2015-01-07 00:39:50 +00004051 // The sign bit is set in both cases: this MUST overflow.
David Majnemer5310c1e2015-01-07 00:39:50 +00004052 return OverflowResult::AlwaysOverflows;
4053 }
4054
Craig Topper6e11a052017-05-08 16:22:48 +00004055 if (LHSKnown.isNonNegative() && RHSKnown.isNonNegative()) {
David Majnemer5310c1e2015-01-07 00:39:50 +00004056 // The sign bit is clear in both cases: this CANNOT overflow.
David Majnemer5310c1e2015-01-07 00:39:50 +00004057 return OverflowResult::NeverOverflows;
4058 }
4059 }
4060
4061 return OverflowResult::MayOverflow;
4062}
James Molloy71b91c22015-05-11 14:42:20 +00004063
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00004064/// Return true if we can prove that adding the two values of the
Craig Topperbb973722017-05-15 02:44:08 +00004065/// knownbits will not overflow.
4066/// Otherwise return false.
4067static bool checkRippleForSignedAdd(const KnownBits &LHSKnown,
4068 const KnownBits &RHSKnown) {
4069 // Addition of two 2's complement numbers having opposite signs will never
4070 // overflow.
4071 if ((LHSKnown.isNegative() && RHSKnown.isNonNegative()) ||
4072 (LHSKnown.isNonNegative() && RHSKnown.isNegative()))
4073 return true;
4074
4075 // If either of the values is known to be non-negative, adding them can only
4076 // overflow if the second is also non-negative, so we can assume that.
Fangrui Songf78650a2018-07-30 19:41:25 +00004077 // Two non-negative numbers will only overflow if there is a carry to the
Craig Topperbb973722017-05-15 02:44:08 +00004078 // sign bit, so we can check if even when the values are as big as possible
4079 // there is no overflow to the sign bit.
4080 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) {
4081 APInt MaxLHS = ~LHSKnown.Zero;
4082 MaxLHS.clearSignBit();
4083 APInt MaxRHS = ~RHSKnown.Zero;
4084 MaxRHS.clearSignBit();
4085 APInt Result = std::move(MaxLHS) + std::move(MaxRHS);
4086 return Result.isSignBitClear();
4087 }
4088
4089 // If either of the values is known to be negative, adding them can only
4090 // overflow if the second is also negative, so we can assume that.
4091 // Two negative number will only overflow if there is no carry to the sign
4092 // bit, so we can check if even when the values are as small as possible
4093 // there is overflow to the sign bit.
4094 if (LHSKnown.isNegative() || RHSKnown.isNegative()) {
4095 APInt MinLHS = LHSKnown.One;
4096 MinLHS.clearSignBit();
4097 APInt MinRHS = RHSKnown.One;
4098 MinRHS.clearSignBit();
4099 APInt Result = std::move(MinLHS) + std::move(MinRHS);
4100 return Result.isSignBitSet();
4101 }
4102
4103 // If we reached here it means that we know nothing about the sign bits.
Fangrui Songf78650a2018-07-30 19:41:25 +00004104 // In this case we can't know if there will be an overflow, since by
Craig Topperbb973722017-05-15 02:44:08 +00004105 // changing the sign bits any two values can be made to overflow.
4106 return false;
4107}
4108
Pete Cooper35b00d52016-08-13 01:05:32 +00004109static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
4110 const Value *RHS,
4111 const AddOperator *Add,
4112 const DataLayout &DL,
Daniel Jasperaec2fa32016-12-19 08:22:17 +00004113 AssumptionCache *AC,
Pete Cooper35b00d52016-08-13 01:05:32 +00004114 const Instruction *CxtI,
4115 const DominatorTree *DT) {
Jingyue Wu10fcea52015-08-20 18:27:04 +00004116 if (Add && Add->hasNoSignedWrap()) {
4117 return OverflowResult::NeverOverflows;
4118 }
4119
Craig Topperbb973722017-05-15 02:44:08 +00004120 // If LHS and RHS each have at least two sign bits, the addition will look
4121 // like
4122 //
4123 // XX..... +
4124 // YY.....
4125 //
4126 // If the carry into the most significant position is 0, X and Y can't both
4127 // be 1 and therefore the carry out of the addition is also 0.
4128 //
4129 // If the carry into the most significant position is 1, X and Y can't both
4130 // be 0 and therefore the carry out of the addition is also 1.
4131 //
4132 // Since the carry into the most significant position is always equal to
4133 // the carry out of the addition, there is no signed overflow.
4134 if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
4135 ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
4136 return OverflowResult::NeverOverflows;
4137
Craig Topper6e11a052017-05-08 16:22:48 +00004138 KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT);
4139 KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT);
Jingyue Wu10fcea52015-08-20 18:27:04 +00004140
Craig Topperbb973722017-05-15 02:44:08 +00004141 if (checkRippleForSignedAdd(LHSKnown, RHSKnown))
Jingyue Wu10fcea52015-08-20 18:27:04 +00004142 return OverflowResult::NeverOverflows;
Jingyue Wu10fcea52015-08-20 18:27:04 +00004143
4144 // The remaining code needs Add to be available. Early returns if not so.
4145 if (!Add)
4146 return OverflowResult::MayOverflow;
4147
4148 // If the sign of Add is the same as at least one of the operands, this add
4149 // CANNOT overflow. This is particularly useful when the sum is
4150 // @llvm.assume'ed non-negative rather than proved so from analyzing its
4151 // operands.
4152 bool LHSOrRHSKnownNonNegative =
Craig Topper6e11a052017-05-08 16:22:48 +00004153 (LHSKnown.isNonNegative() || RHSKnown.isNonNegative());
Fangrui Songf78650a2018-07-30 19:41:25 +00004154 bool LHSOrRHSKnownNegative =
Craig Topperbb973722017-05-15 02:44:08 +00004155 (LHSKnown.isNegative() || RHSKnown.isNegative());
Jingyue Wu10fcea52015-08-20 18:27:04 +00004156 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
Craig Topper6e11a052017-05-08 16:22:48 +00004157 KnownBits AddKnown = computeKnownBits(Add, DL, /*Depth=*/0, AC, CxtI, DT);
4158 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
4159 (AddKnown.isNegative() && LHSOrRHSKnownNegative)) {
Jingyue Wu10fcea52015-08-20 18:27:04 +00004160 return OverflowResult::NeverOverflows;
4161 }
4162 }
4163
4164 return OverflowResult::MayOverflow;
4165}
4166
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +00004167OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
4168 const Value *RHS,
4169 const DataLayout &DL,
4170 AssumptionCache *AC,
4171 const Instruction *CxtI,
4172 const DominatorTree *DT) {
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +00004173 KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT);
Nikita Popovcf596a82018-11-28 16:37:04 +00004174 if (LHSKnown.isNonNegative() || LHSKnown.isNegative()) {
4175 KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT);
4176
4177 // If the LHS is negative and the RHS is non-negative, no unsigned wrap.
4178 if (LHSKnown.isNegative() && RHSKnown.isNonNegative())
4179 return OverflowResult::NeverOverflows;
4180
4181 // If the LHS is non-negative and the RHS negative, we always wrap.
4182 if (LHSKnown.isNonNegative() && RHSKnown.isNegative())
4183 return OverflowResult::AlwaysOverflows;
4184 }
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +00004185
4186 return OverflowResult::MayOverflow;
4187}
4188
4189OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,
4190 const Value *RHS,
4191 const DataLayout &DL,
4192 AssumptionCache *AC,
4193 const Instruction *CxtI,
4194 const DominatorTree *DT) {
4195 // If LHS and RHS each have at least two sign bits, the subtraction
4196 // cannot overflow.
4197 if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
4198 ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
4199 return OverflowResult::NeverOverflows;
4200
4201 KnownBits LHSKnown = computeKnownBits(LHS, DL, 0, AC, CxtI, DT);
4202
4203 KnownBits RHSKnown = computeKnownBits(RHS, DL, 0, AC, CxtI, DT);
4204
4205 // Subtraction of two 2's complement numbers having identical signs will
4206 // never overflow.
4207 if ((LHSKnown.isNegative() && RHSKnown.isNegative()) ||
4208 (LHSKnown.isNonNegative() && RHSKnown.isNonNegative()))
4209 return OverflowResult::NeverOverflows;
4210
4211 // TODO: implement logic similar to checkRippleForAdd
4212 return OverflowResult::MayOverflow;
4213}
4214
Pete Cooper35b00d52016-08-13 01:05:32 +00004215bool llvm::isOverflowIntrinsicNoWrap(const IntrinsicInst *II,
4216 const DominatorTree &DT) {
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004217#ifndef NDEBUG
4218 auto IID = II->getIntrinsicID();
4219 assert((IID == Intrinsic::sadd_with_overflow ||
4220 IID == Intrinsic::uadd_with_overflow ||
4221 IID == Intrinsic::ssub_with_overflow ||
4222 IID == Intrinsic::usub_with_overflow ||
4223 IID == Intrinsic::smul_with_overflow ||
4224 IID == Intrinsic::umul_with_overflow) &&
4225 "Not an overflow intrinsic!");
4226#endif
4227
Pete Cooper35b00d52016-08-13 01:05:32 +00004228 SmallVector<const BranchInst *, 2> GuardingBranches;
4229 SmallVector<const ExtractValueInst *, 2> Results;
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004230
Pete Cooper35b00d52016-08-13 01:05:32 +00004231 for (const User *U : II->users()) {
4232 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004233 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
4234
4235 if (EVI->getIndices()[0] == 0)
4236 Results.push_back(EVI);
4237 else {
4238 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
4239
Pete Cooper35b00d52016-08-13 01:05:32 +00004240 for (const auto *U : EVI->users())
4241 if (const auto *B = dyn_cast<BranchInst>(U)) {
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004242 assert(B->isConditional() && "How else is it using an i1?");
4243 GuardingBranches.push_back(B);
4244 }
4245 }
4246 } else {
4247 // We are using the aggregate directly in a way we don't want to analyze
4248 // here (storing it to a global, say).
4249 return false;
4250 }
4251 }
4252
Pete Cooper35b00d52016-08-13 01:05:32 +00004253 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004254 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
4255 if (!NoWrapEdge.isSingleEdge())
4256 return false;
4257
4258 // Check if all users of the add are provably no-wrap.
Pete Cooper35b00d52016-08-13 01:05:32 +00004259 for (const auto *Result : Results) {
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004260 // If the extractvalue itself is not executed on overflow, the we don't
4261 // need to check each use separately, since domination is transitive.
4262 if (DT.dominates(NoWrapEdge, Result->getParent()))
4263 continue;
4264
4265 for (auto &RU : Result->uses())
4266 if (!DT.dominates(NoWrapEdge, RU))
4267 return false;
4268 }
4269
4270 return true;
4271 };
4272
Eugene Zelenko75075ef2017-09-01 21:37:29 +00004273 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004274}
4275
4276
Pete Cooper35b00d52016-08-13 01:05:32 +00004277OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add,
Jingyue Wu10fcea52015-08-20 18:27:04 +00004278 const DataLayout &DL,
Daniel Jasperaec2fa32016-12-19 08:22:17 +00004279 AssumptionCache *AC,
Jingyue Wu10fcea52015-08-20 18:27:04 +00004280 const Instruction *CxtI,
4281 const DominatorTree *DT) {
4282 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
Daniel Jasperaec2fa32016-12-19 08:22:17 +00004283 Add, DL, AC, CxtI, DT);
Jingyue Wu10fcea52015-08-20 18:27:04 +00004284}
4285
Pete Cooper35b00d52016-08-13 01:05:32 +00004286OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS,
4287 const Value *RHS,
Jingyue Wu10fcea52015-08-20 18:27:04 +00004288 const DataLayout &DL,
Daniel Jasperaec2fa32016-12-19 08:22:17 +00004289 AssumptionCache *AC,
Jingyue Wu10fcea52015-08-20 18:27:04 +00004290 const Instruction *CxtI,
4291 const DominatorTree *DT) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00004292 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT);
Jingyue Wu10fcea52015-08-20 18:27:04 +00004293}
4294
Jingyue Wu42f1d672015-07-28 18:22:40 +00004295bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {
Eli Friedmanf1da33e2016-06-11 21:48:25 +00004296 // A memory operation returns normally if it isn't volatile. A volatile
4297 // operation is allowed to trap.
4298 //
4299 // An atomic operation isn't guaranteed to return in a reasonable amount of
4300 // time because it's possible for another thread to interfere with it for an
4301 // arbitrary length of time, but programs aren't allowed to rely on that.
4302 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
4303 return !LI->isVolatile();
4304 if (const StoreInst *SI = dyn_cast<StoreInst>(I))
4305 return !SI->isVolatile();
4306 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I))
4307 return !CXI->isVolatile();
4308 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I))
4309 return !RMWI->isVolatile();
4310 if (const MemIntrinsic *MII = dyn_cast<MemIntrinsic>(I))
4311 return !MII->isVolatile();
Jingyue Wu42f1d672015-07-28 18:22:40 +00004312
Eli Friedmanf1da33e2016-06-11 21:48:25 +00004313 // If there is no successor, then execution can't transfer to it.
4314 if (const auto *CRI = dyn_cast<CleanupReturnInst>(I))
4315 return !CRI->unwindsToCaller();
4316 if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(I))
4317 return !CatchSwitch->unwindsToCaller();
4318 if (isa<ResumeInst>(I))
4319 return false;
4320 if (isa<ReturnInst>(I))
4321 return false;
Sebastian Pop4a4d2452017-03-08 01:54:50 +00004322 if (isa<UnreachableInst>(I))
4323 return false;
Sanjoy Das9a65cd22016-06-08 17:48:36 +00004324
Eli Friedmanf1da33e2016-06-11 21:48:25 +00004325 // Calls can throw, or contain an infinite loop, or kill the process.
Sanjoy Das09455302016-12-31 22:12:31 +00004326 if (auto CS = ImmutableCallSite(I)) {
Sanjoy Das3bb2dbd2016-12-31 22:12:34 +00004327 // Call sites that throw have implicit non-local control flow.
4328 if (!CS.doesNotThrow())
4329 return false;
4330
4331 // Non-throwing call sites can loop infinitely, call exit/pthread_exit
4332 // etc. and thus not return. However, LLVM already assumes that
4333 //
4334 // - Thread exiting actions are modeled as writes to memory invisible to
4335 // the program.
4336 //
4337 // - Loops that don't have side effects (side effects are volatile/atomic
4338 // stores and IO) always terminate (see http://llvm.org/PR965).
4339 // Furthermore IO itself is also modeled as writes to memory invisible to
4340 // the program.
4341 //
4342 // We rely on those assumptions here, and use the memory effects of the call
4343 // target as a proxy for checking that it always returns.
4344
4345 // FIXME: This isn't aggressive enough; a call which only writes to a global
4346 // is guaranteed to return.
Sanjoy Dasd7e82062016-06-14 20:23:16 +00004347 return CS.onlyReadsMemory() || CS.onlyAccessesArgMemory() ||
Dan Gohman2c74fe92017-11-08 21:59:51 +00004348 match(I, m_Intrinsic<Intrinsic::assume>()) ||
4349 match(I, m_Intrinsic<Intrinsic::sideeffect>());
Eli Friedmanf1da33e2016-06-11 21:48:25 +00004350 }
4351
4352 // Other instructions return normally.
4353 return true;
Jingyue Wu42f1d672015-07-28 18:22:40 +00004354}
4355
Philip Reamesfbffd122018-03-08 21:25:30 +00004356bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) {
Hiroshi Inouec437f312019-01-30 05:26:31 +00004357 // TODO: This is slightly conservative for invoke instruction since exiting
Philip Reamesfbffd122018-03-08 21:25:30 +00004358 // via an exception *is* normal control for them.
4359 for (auto I = BB->begin(), E = BB->end(); I != E; ++I)
4360 if (!isGuaranteedToTransferExecutionToSuccessor(&*I))
4361 return false;
4362 return true;
4363}
4364
Jingyue Wu42f1d672015-07-28 18:22:40 +00004365bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,
4366 const Loop *L) {
4367 // The loop header is guaranteed to be executed for every iteration.
4368 //
4369 // FIXME: Relax this constraint to cover all basic blocks that are
4370 // guaranteed to be executed at every iteration.
4371 if (I->getParent() != L->getHeader()) return false;
4372
4373 for (const Instruction &LI : *L->getHeader()) {
4374 if (&LI == I) return true;
4375 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
4376 }
4377 llvm_unreachable("Instruction not contained in its own parent basic block.");
4378}
4379
4380bool llvm::propagatesFullPoison(const Instruction *I) {
4381 switch (I->getOpcode()) {
Sanjoy Das7b0b4082017-02-21 02:42:42 +00004382 case Instruction::Add:
4383 case Instruction::Sub:
4384 case Instruction::Xor:
4385 case Instruction::Trunc:
4386 case Instruction::BitCast:
4387 case Instruction::AddrSpaceCast:
Sanjoy Das5cd6c5ca2017-02-22 06:52:32 +00004388 case Instruction::Mul:
4389 case Instruction::Shl:
4390 case Instruction::GetElementPtr:
Sanjoy Das7b0b4082017-02-21 02:42:42 +00004391 // These operations all propagate poison unconditionally. Note that poison
4392 // is not any particular value, so xor or subtraction of poison with
4393 // itself still yields poison, not zero.
4394 return true;
Jingyue Wu42f1d672015-07-28 18:22:40 +00004395
Sanjoy Das7b0b4082017-02-21 02:42:42 +00004396 case Instruction::AShr:
4397 case Instruction::SExt:
4398 // For these operations, one bit of the input is replicated across
4399 // multiple output bits. A replicated poison bit is still poison.
4400 return true;
Jingyue Wu42f1d672015-07-28 18:22:40 +00004401
Sanjoy Das7b0b4082017-02-21 02:42:42 +00004402 case Instruction::ICmp:
4403 // Comparing poison with any value yields poison. This is why, for
4404 // instance, x s< (x +nsw 1) can be folded to true.
4405 return true;
Sanjoy Das70c2bbd2016-05-29 00:31:18 +00004406
Sanjoy Das7b0b4082017-02-21 02:42:42 +00004407 default:
4408 return false;
Jingyue Wu42f1d672015-07-28 18:22:40 +00004409 }
4410}
4411
4412const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) {
4413 switch (I->getOpcode()) {
4414 case Instruction::Store:
4415 return cast<StoreInst>(I)->getPointerOperand();
4416
4417 case Instruction::Load:
4418 return cast<LoadInst>(I)->getPointerOperand();
4419
4420 case Instruction::AtomicCmpXchg:
4421 return cast<AtomicCmpXchgInst>(I)->getPointerOperand();
4422
4423 case Instruction::AtomicRMW:
4424 return cast<AtomicRMWInst>(I)->getPointerOperand();
4425
4426 case Instruction::UDiv:
4427 case Instruction::SDiv:
4428 case Instruction::URem:
4429 case Instruction::SRem:
4430 return I->getOperand(1);
4431
4432 default:
4433 return nullptr;
4434 }
4435}
4436
Sanjoy Das08989c72017-04-30 19:41:19 +00004437bool llvm::programUndefinedIfFullPoison(const Instruction *PoisonI) {
Jingyue Wu42f1d672015-07-28 18:22:40 +00004438 // We currently only look for uses of poison values within the same basic
4439 // block, as that makes it easier to guarantee that the uses will be
4440 // executed given that PoisonI is executed.
4441 //
4442 // FIXME: Expand this to consider uses beyond the same basic block. To do
4443 // this, look out for the distinction between post-dominance and strong
4444 // post-dominance.
4445 const BasicBlock *BB = PoisonI->getParent();
4446
4447 // Set of instructions that we have proved will yield poison if PoisonI
4448 // does.
4449 SmallSet<const Value *, 16> YieldsPoison;
Sanjoy Dasa6155b62016-04-22 17:41:06 +00004450 SmallSet<const BasicBlock *, 4> Visited;
Jingyue Wu42f1d672015-07-28 18:22:40 +00004451 YieldsPoison.insert(PoisonI);
Sanjoy Dasa6155b62016-04-22 17:41:06 +00004452 Visited.insert(PoisonI->getParent());
Jingyue Wu42f1d672015-07-28 18:22:40 +00004453
Sanjoy Dasa6155b62016-04-22 17:41:06 +00004454 BasicBlock::const_iterator Begin = PoisonI->getIterator(), End = BB->end();
Jingyue Wu42f1d672015-07-28 18:22:40 +00004455
Sanjoy Dasa6155b62016-04-22 17:41:06 +00004456 unsigned Iter = 0;
4457 while (Iter++ < MaxDepth) {
4458 for (auto &I : make_range(Begin, End)) {
4459 if (&I != PoisonI) {
4460 const Value *NotPoison = getGuaranteedNonFullPoisonOp(&I);
4461 if (NotPoison != nullptr && YieldsPoison.count(NotPoison))
4462 return true;
4463 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
4464 return false;
4465 }
4466
4467 // Mark poison that propagates from I through uses of I.
4468 if (YieldsPoison.count(&I)) {
4469 for (const User *User : I.users()) {
4470 const Instruction *UserI = cast<Instruction>(User);
4471 if (propagatesFullPoison(UserI))
4472 YieldsPoison.insert(User);
4473 }
Jingyue Wu42f1d672015-07-28 18:22:40 +00004474 }
4475 }
Sanjoy Dasa6155b62016-04-22 17:41:06 +00004476
4477 if (auto *NextBB = BB->getSingleSuccessor()) {
4478 if (Visited.insert(NextBB).second) {
4479 BB = NextBB;
4480 Begin = BB->getFirstNonPHI()->getIterator();
4481 End = BB->end();
4482 continue;
4483 }
4484 }
4485
4486 break;
Eugene Zelenko75075ef2017-09-01 21:37:29 +00004487 }
Jingyue Wu42f1d672015-07-28 18:22:40 +00004488 return false;
4489}
4490
Pete Cooper35b00d52016-08-13 01:05:32 +00004491static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
James Molloy134bec22015-08-11 09:12:57 +00004492 if (FMF.noNaNs())
4493 return true;
4494
4495 if (auto *C = dyn_cast<ConstantFP>(V))
4496 return !C->isNaN();
Thomas Livelyd47b5c72018-09-28 21:36:43 +00004497
4498 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
4499 if (!C->getElementType()->isFloatingPointTy())
4500 return false;
4501 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
4502 if (C->getElementAsAPFloat(I).isNaN())
4503 return false;
4504 }
4505 return true;
4506 }
4507
James Molloy134bec22015-08-11 09:12:57 +00004508 return false;
4509}
4510
Pete Cooper35b00d52016-08-13 01:05:32 +00004511static bool isKnownNonZero(const Value *V) {
James Molloy134bec22015-08-11 09:12:57 +00004512 if (auto *C = dyn_cast<ConstantFP>(V))
4513 return !C->isZero();
Thomas Livelyd47b5c72018-09-28 21:36:43 +00004514
4515 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
4516 if (!C->getElementType()->isFloatingPointTy())
4517 return false;
4518 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
4519 if (C->getElementAsAPFloat(I).isZero())
4520 return false;
4521 }
4522 return true;
4523 }
4524
James Molloy134bec22015-08-11 09:12:57 +00004525 return false;
4526}
4527
Nikolai Bozhenov1545eb32017-08-04 12:22:17 +00004528/// Match clamp pattern for float types without care about NaNs or signed zeros.
4529/// Given non-min/max outer cmp/select from the clamp pattern this
4530/// function recognizes if it can be substitued by a "canonical" min/max
4531/// pattern.
4532static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred,
4533 Value *CmpLHS, Value *CmpRHS,
4534 Value *TrueVal, Value *FalseVal,
4535 Value *&LHS, Value *&RHS) {
4536 // Try to match
4537 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
4538 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
4539 // and return description of the outer Max/Min.
4540
4541 // First, check if select has inverse order:
4542 if (CmpRHS == FalseVal) {
4543 std::swap(TrueVal, FalseVal);
4544 Pred = CmpInst::getInversePredicate(Pred);
4545 }
4546
4547 // Assume success now. If there's no match, callers should not use these anyway.
4548 LHS = TrueVal;
4549 RHS = FalseVal;
4550
4551 const APFloat *FC1;
4552 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
4553 return {SPF_UNKNOWN, SPNB_NA, false};
4554
4555 const APFloat *FC2;
4556 switch (Pred) {
4557 case CmpInst::FCMP_OLT:
4558 case CmpInst::FCMP_OLE:
4559 case CmpInst::FCMP_ULT:
4560 case CmpInst::FCMP_ULE:
4561 if (match(FalseVal,
4562 m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)),
4563 m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
4564 FC1->compare(*FC2) == APFloat::cmpResult::cmpLessThan)
4565 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
4566 break;
4567 case CmpInst::FCMP_OGT:
4568 case CmpInst::FCMP_OGE:
4569 case CmpInst::FCMP_UGT:
4570 case CmpInst::FCMP_UGE:
4571 if (match(FalseVal,
4572 m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)),
4573 m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
4574 FC1->compare(*FC2) == APFloat::cmpResult::cmpGreaterThan)
4575 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
4576 break;
4577 default:
4578 break;
4579 }
4580
4581 return {SPF_UNKNOWN, SPNB_NA, false};
4582}
4583
Artur Gainullinaf7ba8f2017-10-27 20:53:41 +00004584/// Recognize variations of:
4585/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
4586static SelectPatternResult matchClamp(CmpInst::Predicate Pred,
4587 Value *CmpLHS, Value *CmpRHS,
4588 Value *TrueVal, Value *FalseVal) {
4589 // Swap the select operands and predicate to match the patterns below.
4590 if (CmpRHS != TrueVal) {
4591 Pred = ICmpInst::getSwappedPredicate(Pred);
4592 std::swap(TrueVal, FalseVal);
4593 }
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004594 const APInt *C1;
4595 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
4596 const APInt *C2;
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004597 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
4598 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004599 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004600 return {SPF_SMAX, SPNB_NA, false};
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004601
4602 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
4603 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004604 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004605 return {SPF_SMIN, SPNB_NA, false};
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004606
4607 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
4608 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004609 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004610 return {SPF_UMAX, SPNB_NA, false};
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004611
4612 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
4613 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004614 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004615 return {SPF_UMIN, SPNB_NA, false};
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004616 }
Artur Gainullinaf7ba8f2017-10-27 20:53:41 +00004617 return {SPF_UNKNOWN, SPNB_NA, false};
4618}
4619
Sanjay Patel78114302018-01-02 20:56:45 +00004620/// Recognize variations of:
4621/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
4622static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred,
4623 Value *CmpLHS, Value *CmpRHS,
Sanjay Patel1d91ec32018-01-24 15:20:37 +00004624 Value *TVal, Value *FVal,
4625 unsigned Depth) {
Sanjay Patel78114302018-01-02 20:56:45 +00004626 // TODO: Allow FP min/max with nnan/nsz.
4627 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
4628
4629 Value *A, *B;
Sanjay Patel1d91ec32018-01-24 15:20:37 +00004630 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
Sanjay Patel78114302018-01-02 20:56:45 +00004631 if (!SelectPatternResult::isMinOrMax(L.Flavor))
4632 return {SPF_UNKNOWN, SPNB_NA, false};
4633
4634 Value *C, *D;
Sanjay Patel1d91ec32018-01-24 15:20:37 +00004635 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
Sanjay Patel78114302018-01-02 20:56:45 +00004636 if (L.Flavor != R.Flavor)
4637 return {SPF_UNKNOWN, SPNB_NA, false};
4638
Sanjay Patele63d8dd2018-01-11 15:13:47 +00004639 // We have something like: x Pred y ? min(a, b) : min(c, d).
4640 // Try to match the compare to the min/max operations of the select operands.
4641 // First, make sure we have the right compare predicate.
Sanjay Patel78114302018-01-02 20:56:45 +00004642 switch (L.Flavor) {
4643 case SPF_SMIN:
4644 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
4645 Pred = ICmpInst::getSwappedPredicate(Pred);
4646 std::swap(CmpLHS, CmpRHS);
4647 }
4648 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
4649 break;
4650 return {SPF_UNKNOWN, SPNB_NA, false};
4651 case SPF_SMAX:
4652 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
4653 Pred = ICmpInst::getSwappedPredicate(Pred);
4654 std::swap(CmpLHS, CmpRHS);
4655 }
4656 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
4657 break;
4658 return {SPF_UNKNOWN, SPNB_NA, false};
4659 case SPF_UMIN:
4660 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
4661 Pred = ICmpInst::getSwappedPredicate(Pred);
4662 std::swap(CmpLHS, CmpRHS);
4663 }
4664 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
4665 break;
4666 return {SPF_UNKNOWN, SPNB_NA, false};
4667 case SPF_UMAX:
4668 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
4669 Pred = ICmpInst::getSwappedPredicate(Pred);
4670 std::swap(CmpLHS, CmpRHS);
4671 }
4672 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
4673 break;
4674 return {SPF_UNKNOWN, SPNB_NA, false};
4675 default:
Sanjay Patel7dfe96a2018-01-08 18:31:13 +00004676 return {SPF_UNKNOWN, SPNB_NA, false};
Sanjay Patel78114302018-01-02 20:56:45 +00004677 }
4678
Sanjay Patele63d8dd2018-01-11 15:13:47 +00004679 // If there is a common operand in the already matched min/max and the other
4680 // min/max operands match the compare operands (either directly or inverted),
4681 // then this is min/max of the same flavor.
4682
Sanjay Patel78114302018-01-02 20:56:45 +00004683 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
Sanjay Patele63d8dd2018-01-11 15:13:47 +00004684 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
4685 if (D == B) {
4686 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
4687 match(A, m_Not(m_Specific(CmpRHS)))))
4688 return {L.Flavor, SPNB_NA, false};
4689 }
Sanjay Patel78114302018-01-02 20:56:45 +00004690 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
Sanjay Patele63d8dd2018-01-11 15:13:47 +00004691 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
4692 if (C == B) {
4693 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
4694 match(A, m_Not(m_Specific(CmpRHS)))))
4695 return {L.Flavor, SPNB_NA, false};
4696 }
Sanjay Patel78114302018-01-02 20:56:45 +00004697 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
Sanjay Patele63d8dd2018-01-11 15:13:47 +00004698 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
4699 if (D == A) {
4700 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
4701 match(B, m_Not(m_Specific(CmpRHS)))))
4702 return {L.Flavor, SPNB_NA, false};
4703 }
Sanjay Patel78114302018-01-02 20:56:45 +00004704 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
Sanjay Patele63d8dd2018-01-11 15:13:47 +00004705 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
4706 if (C == A) {
4707 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
4708 match(B, m_Not(m_Specific(CmpRHS)))))
4709 return {L.Flavor, SPNB_NA, false};
4710 }
Sanjay Patel78114302018-01-02 20:56:45 +00004711
4712 return {SPF_UNKNOWN, SPNB_NA, false};
4713}
4714
Artur Gainullinaf7ba8f2017-10-27 20:53:41 +00004715/// Match non-obvious integer minimum and maximum sequences.
4716static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
4717 Value *CmpLHS, Value *CmpRHS,
4718 Value *TrueVal, Value *FalseVal,
Sanjay Patel1d91ec32018-01-24 15:20:37 +00004719 Value *&LHS, Value *&RHS,
4720 unsigned Depth) {
Artur Gainullinaf7ba8f2017-10-27 20:53:41 +00004721 // Assume success. If there's no match, callers should not use these anyway.
4722 LHS = TrueVal;
4723 RHS = FalseVal;
4724
4725 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
4726 if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
4727 return SPR;
Sanjay Patel0c1c70a2017-01-20 22:18:47 +00004728
Sanjay Patel1d91ec32018-01-24 15:20:37 +00004729 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
Sanjay Patel78114302018-01-02 20:56:45 +00004730 if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
4731 return SPR;
Fangrui Songf78650a2018-07-30 19:41:25 +00004732
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004733 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
Sanjay Patel819f0962016-11-13 19:30:19 +00004734 return {SPF_UNKNOWN, SPNB_NA, false};
4735
Sanjay Patelcfcc42b2016-11-13 20:04:52 +00004736 // Z = X -nsw Y
4737 // (X >s Y) ? 0 : Z ==> (Z >s 0) ? 0 : Z ==> SMIN(Z, 0)
4738 // (X <s Y) ? 0 : Z ==> (Z <s 0) ? 0 : Z ==> SMAX(Z, 0)
4739 if (match(TrueVal, m_Zero()) &&
Sanjay Patel24c6f882017-01-21 17:51:25 +00004740 match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004741 return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
Sanjay Patelcfcc42b2016-11-13 20:04:52 +00004742
4743 // Z = X -nsw Y
4744 // (X >s Y) ? Z : 0 ==> (Z >s 0) ? Z : 0 ==> SMAX(Z, 0)
4745 // (X <s Y) ? Z : 0 ==> (Z <s 0) ? Z : 0 ==> SMIN(Z, 0)
4746 if (match(FalseVal, m_Zero()) &&
Sanjay Patel24c6f882017-01-21 17:51:25 +00004747 match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004748 return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
Sanjay Patelcfcc42b2016-11-13 20:04:52 +00004749
Artur Gainullinaf7ba8f2017-10-27 20:53:41 +00004750 const APInt *C1;
Sanjay Patel819f0962016-11-13 19:30:19 +00004751 if (!match(CmpRHS, m_APInt(C1)))
4752 return {SPF_UNKNOWN, SPNB_NA, false};
4753
4754 // An unsigned min/max can be written with a signed compare.
4755 const APInt *C2;
4756 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
4757 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
4758 // Is the sign bit set?
4759 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
4760 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
Craig Topper81d772c2017-11-08 19:38:45 +00004761 if (Pred == CmpInst::ICMP_SLT && C1->isNullValue() &&
4762 C2->isMaxSignedValue())
Sanjay Patel819f0962016-11-13 19:30:19 +00004763 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
Sanjay Patel819f0962016-11-13 19:30:19 +00004764
4765 // Is the sign bit clear?
4766 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
4767 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004768 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnesValue() &&
4769 C2->isMinSignedValue())
Sanjay Patel819f0962016-11-13 19:30:19 +00004770 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
Sanjay Patel819f0962016-11-13 19:30:19 +00004771 }
4772
4773 // Look through 'not' ops to find disguised signed min/max.
4774 // (X >s C) ? ~X : ~C ==> (~X <s ~C) ? ~X : ~C ==> SMIN(~X, ~C)
4775 // (X <s C) ? ~X : ~C ==> (~X >s ~C) ? ~X : ~C ==> SMAX(~X, ~C)
4776 if (match(TrueVal, m_Not(m_Specific(CmpLHS))) &&
Sanjay Patel24c6f882017-01-21 17:51:25 +00004777 match(FalseVal, m_APInt(C2)) && ~(*C1) == *C2)
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004778 return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
Sanjay Patel819f0962016-11-13 19:30:19 +00004779
4780 // (X >s C) ? ~C : ~X ==> (~X <s ~C) ? ~C : ~X ==> SMAX(~C, ~X)
4781 // (X <s C) ? ~C : ~X ==> (~X >s ~C) ? ~C : ~X ==> SMIN(~C, ~X)
4782 if (match(FalseVal, m_Not(m_Specific(CmpLHS))) &&
Sanjay Patel24c6f882017-01-21 17:51:25 +00004783 match(TrueVal, m_APInt(C2)) && ~(*C1) == *C2)
Nikolai Bozhenov8dcab542017-10-19 15:36:18 +00004784 return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
Sanjay Patel819f0962016-11-13 19:30:19 +00004785
4786 return {SPF_UNKNOWN, SPNB_NA, false};
4787}
4788
Chen Zheng69bb0642018-07-21 12:27:54 +00004789bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) {
Chen Zhengfdf13ef2018-07-12 03:06:04 +00004790 assert(X && Y && "Invalid operand");
4791
Chen Zheng69bb0642018-07-21 12:27:54 +00004792 // X = sub (0, Y) || X = sub nsw (0, Y)
4793 if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) ||
4794 (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y)))))
Chen Zhengfdf13ef2018-07-12 03:06:04 +00004795 return true;
4796
Chen Zheng69bb0642018-07-21 12:27:54 +00004797 // Y = sub (0, X) || Y = sub nsw (0, X)
4798 if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) ||
4799 (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X)))))
Chen Zhengfdf13ef2018-07-12 03:06:04 +00004800 return true;
4801
Chen Zheng69bb0642018-07-21 12:27:54 +00004802 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
Chen Zhengfdf13ef2018-07-12 03:06:04 +00004803 Value *A, *B;
Chen Zheng69bb0642018-07-21 12:27:54 +00004804 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
4805 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
4806 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
4807 match(Y, m_NSWSub(m_Specific(B), m_Specific(A)))));
Chen Zhengfdf13ef2018-07-12 03:06:04 +00004808}
4809
James Molloy134bec22015-08-11 09:12:57 +00004810static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
4811 FastMathFlags FMF,
James Molloy270ef8c2015-05-15 16:04:50 +00004812 Value *CmpLHS, Value *CmpRHS,
4813 Value *TrueVal, Value *FalseVal,
Sanjay Patel1d91ec32018-01-24 15:20:37 +00004814 Value *&LHS, Value *&RHS,
4815 unsigned Depth) {
Sanjay Patele7c94ef2018-11-04 14:28:48 +00004816 if (CmpInst::isFPPredicate(Pred)) {
4817 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
4818 // 0.0 operand, set the compare's 0.0 operands to that same value for the
4819 // purpose of identifying min/max. Disregard vector constants with undefined
4820 // elements because those can not be back-propagated for analysis.
4821 Value *OutputZeroVal = nullptr;
4822 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
4823 !cast<Constant>(TrueVal)->containsUndefElement())
4824 OutputZeroVal = TrueVal;
4825 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
4826 !cast<Constant>(FalseVal)->containsUndefElement())
4827 OutputZeroVal = FalseVal;
4828
4829 if (OutputZeroVal) {
4830 if (match(CmpLHS, m_AnyZeroFP()))
4831 CmpLHS = OutputZeroVal;
4832 if (match(CmpRHS, m_AnyZeroFP()))
4833 CmpRHS = OutputZeroVal;
4834 }
4835 }
4836
James Molloy71b91c22015-05-11 14:42:20 +00004837 LHS = CmpLHS;
4838 RHS = CmpRHS;
4839
Sanjay Patel9a399792017-12-26 15:09:19 +00004840 // Signed zero may return inconsistent results between implementations.
4841 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
4842 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
4843 // Therefore, we behave conservatively and only proceed if at least one of the
4844 // operands is known to not be zero or if we don't care about signed zero.
James Molloy134bec22015-08-11 09:12:57 +00004845 switch (Pred) {
4846 default: break;
Sanjay Patel9a399792017-12-26 15:09:19 +00004847 // FIXME: Include OGT/OLT/UGT/ULT.
James Molloy134bec22015-08-11 09:12:57 +00004848 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE:
4849 case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE:
4850 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
4851 !isKnownNonZero(CmpRHS))
4852 return {SPF_UNKNOWN, SPNB_NA, false};
4853 }
4854
4855 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
4856 bool Ordered = false;
4857
4858 // When given one NaN and one non-NaN input:
4859 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
4860 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
4861 // ordered comparison fails), which could be NaN or non-NaN.
4862 // so here we discover exactly what NaN behavior is required/accepted.
4863 if (CmpInst::isFPPredicate(Pred)) {
4864 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
4865 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
4866
4867 if (LHSSafe && RHSSafe) {
4868 // Both operands are known non-NaN.
4869 NaNBehavior = SPNB_RETURNS_ANY;
4870 } else if (CmpInst::isOrdered(Pred)) {
4871 // An ordered comparison will return false when given a NaN, so it
4872 // returns the RHS.
4873 Ordered = true;
4874 if (LHSSafe)
James Molloy8990b062015-08-12 15:11:43 +00004875 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
James Molloy134bec22015-08-11 09:12:57 +00004876 NaNBehavior = SPNB_RETURNS_NAN;
4877 else if (RHSSafe)
4878 NaNBehavior = SPNB_RETURNS_OTHER;
4879 else
4880 // Completely unsafe.
4881 return {SPF_UNKNOWN, SPNB_NA, false};
4882 } else {
4883 Ordered = false;
4884 // An unordered comparison will return true when given a NaN, so it
4885 // returns the LHS.
4886 if (LHSSafe)
James Molloy8990b062015-08-12 15:11:43 +00004887 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
James Molloy134bec22015-08-11 09:12:57 +00004888 NaNBehavior = SPNB_RETURNS_OTHER;
4889 else if (RHSSafe)
4890 NaNBehavior = SPNB_RETURNS_NAN;
4891 else
4892 // Completely unsafe.
4893 return {SPF_UNKNOWN, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00004894 }
4895 }
4896
James Molloy71b91c22015-05-11 14:42:20 +00004897 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
James Molloy134bec22015-08-11 09:12:57 +00004898 std::swap(CmpLHS, CmpRHS);
4899 Pred = CmpInst::getSwappedPredicate(Pred);
4900 if (NaNBehavior == SPNB_RETURNS_NAN)
4901 NaNBehavior = SPNB_RETURNS_OTHER;
4902 else if (NaNBehavior == SPNB_RETURNS_OTHER)
4903 NaNBehavior = SPNB_RETURNS_NAN;
4904 Ordered = !Ordered;
4905 }
4906
4907 // ([if]cmp X, Y) ? X : Y
4908 if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
James Molloy71b91c22015-05-11 14:42:20 +00004909 switch (Pred) {
James Molloy134bec22015-08-11 09:12:57 +00004910 default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
James Molloy71b91c22015-05-11 14:42:20 +00004911 case ICmpInst::ICMP_UGT:
James Molloy134bec22015-08-11 09:12:57 +00004912 case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00004913 case ICmpInst::ICMP_SGT:
James Molloy134bec22015-08-11 09:12:57 +00004914 case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00004915 case ICmpInst::ICMP_ULT:
James Molloy134bec22015-08-11 09:12:57 +00004916 case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00004917 case ICmpInst::ICMP_SLT:
James Molloy134bec22015-08-11 09:12:57 +00004918 case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
4919 case FCmpInst::FCMP_UGT:
4920 case FCmpInst::FCMP_UGE:
4921 case FCmpInst::FCMP_OGT:
4922 case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
4923 case FCmpInst::FCMP_ULT:
4924 case FCmpInst::FCMP_ULE:
4925 case FCmpInst::FCMP_OLT:
4926 case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
James Molloy71b91c22015-05-11 14:42:20 +00004927 }
4928 }
Fangrui Songf78650a2018-07-30 19:41:25 +00004929
Chen Zhengccc84222018-07-16 02:23:00 +00004930 if (isKnownNegation(TrueVal, FalseVal)) {
4931 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
4932 // match against either LHS or sext(LHS).
4933 auto MaybeSExtCmpLHS =
4934 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
4935 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
4936 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
4937 if (match(TrueVal, MaybeSExtCmpLHS)) {
4938 // Set the return values. If the compare uses the negated value (-X >s 0),
4939 // swap the return values because the negated value is always 'RHS'.
Sanjay Patel284ba0c2018-07-02 14:43:40 +00004940 LHS = TrueVal;
4941 RHS = FalseVal;
Chen Zhengccc84222018-07-16 02:23:00 +00004942 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
4943 std::swap(LHS, RHS);
4944
4945 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
4946 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
4947 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
4948 return {SPF_ABS, SPNB_NA, false};
4949
4950 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
4951 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
4952 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
4953 return {SPF_NABS, SPNB_NA, false};
4954 }
4955 else if (match(FalseVal, MaybeSExtCmpLHS)) {
4956 // Set the return values. If the compare uses the negated value (-X >s 0),
4957 // swap the return values because the negated value is always 'RHS'.
Sanjay Patel284ba0c2018-07-02 14:43:40 +00004958 LHS = FalseVal;
4959 RHS = TrueVal;
Chen Zhengccc84222018-07-16 02:23:00 +00004960 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
4961 std::swap(LHS, RHS);
4962
4963 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
4964 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
4965 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
4966 return {SPF_NABS, SPNB_NA, false};
4967
4968 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
4969 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
4970 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
4971 return {SPF_ABS, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00004972 }
James Molloy71b91c22015-05-11 14:42:20 +00004973 }
4974
Nikolai Bozhenov1545eb32017-08-04 12:22:17 +00004975 if (CmpInst::isIntPredicate(Pred))
Sanjay Patel1d91ec32018-01-24 15:20:37 +00004976 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
Nikolai Bozhenov1545eb32017-08-04 12:22:17 +00004977
4978 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
4979 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
4980 // semantics than minNum. Be conservative in such case.
4981 if (NaNBehavior != SPNB_RETURNS_ANY ||
4982 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
4983 !isKnownNonZero(CmpRHS)))
4984 return {SPF_UNKNOWN, SPNB_NA, false};
4985
4986 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
James Molloy71b91c22015-05-11 14:42:20 +00004987}
James Molloy270ef8c2015-05-15 16:04:50 +00004988
Nikolai Bozhenov74c047e2017-10-18 09:28:09 +00004989/// Helps to match a select pattern in case of a type mismatch.
4990///
4991/// The function processes the case when type of true and false values of a
4992/// select instruction differs from type of the cmp instruction operands because
Vedant Kumar1a8456d2018-03-02 18:57:02 +00004993/// of a cast instruction. The function checks if it is legal to move the cast
Nikolai Bozhenov74c047e2017-10-18 09:28:09 +00004994/// operation after "select". If yes, it returns the new second value of
4995/// "select" (with the assumption that cast is moved):
4996/// 1. As operand of cast instruction when both values of "select" are same cast
4997/// instructions.
4998/// 2. As restored constant (by applying reverse cast operation) when the first
4999/// value of the "select" is a cast operation and the second value is a
5000/// constant.
5001/// NOTE: We return only the new second value because the first value could be
5002/// accessed as operand of cast instruction.
James Molloy569cea62015-09-02 17:25:25 +00005003static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
5004 Instruction::CastOps *CastOp) {
Sanjay Patel14a4b812017-01-29 16:34:57 +00005005 auto *Cast1 = dyn_cast<CastInst>(V1);
5006 if (!Cast1)
James Molloy270ef8c2015-05-15 16:04:50 +00005007 return nullptr;
James Molloy270ef8c2015-05-15 16:04:50 +00005008
Sanjay Patel14a4b812017-01-29 16:34:57 +00005009 *CastOp = Cast1->getOpcode();
5010 Type *SrcTy = Cast1->getSrcTy();
5011 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
5012 // If V1 and V2 are both the same cast from the same type, look through V1.
5013 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
5014 return Cast2->getOperand(0);
James Molloy569cea62015-09-02 17:25:25 +00005015 return nullptr;
5016 }
5017
Sanjay Patel14a4b812017-01-29 16:34:57 +00005018 auto *C = dyn_cast<Constant>(V2);
5019 if (!C)
5020 return nullptr;
5021
David Majnemerd2a074b2016-04-29 18:40:34 +00005022 Constant *CastedTo = nullptr;
Sanjay Patel14a4b812017-01-29 16:34:57 +00005023 switch (*CastOp) {
5024 case Instruction::ZExt:
5025 if (CmpI->isUnsigned())
5026 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
5027 break;
5028 case Instruction::SExt:
5029 if (CmpI->isSigned())
5030 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
5031 break;
5032 case Instruction::Trunc:
Nikolai Bozhenov74c047e2017-10-18 09:28:09 +00005033 Constant *CmpConst;
Nikolai Bozhenov9723f122017-10-18 14:24:50 +00005034 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
5035 CmpConst->getType() == SrcTy) {
Nikolai Bozhenov74c047e2017-10-18 09:28:09 +00005036 // Here we have the following case:
5037 //
5038 // %cond = cmp iN %x, CmpConst
5039 // %tr = trunc iN %x to iK
5040 // %narrowsel = select i1 %cond, iK %t, iK C
5041 //
5042 // We can always move trunc after select operation:
5043 //
5044 // %cond = cmp iN %x, CmpConst
5045 // %widesel = select i1 %cond, iN %x, iN CmpConst
5046 // %tr = trunc iN %widesel to iK
5047 //
5048 // Note that C could be extended in any way because we don't care about
5049 // upper bits after truncation. It can't be abs pattern, because it would
5050 // look like:
5051 //
5052 // select i1 %cond, x, -x.
5053 //
5054 // So only min/max pattern could be matched. Such match requires widened C
5055 // == CmpConst. That is why set widened C = CmpConst, condition trunc
5056 // CmpConst == C is checked below.
5057 CastedTo = CmpConst;
5058 } else {
5059 CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned());
5060 }
Sanjay Patel14a4b812017-01-29 16:34:57 +00005061 break;
5062 case Instruction::FPTrunc:
5063 CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true);
5064 break;
5065 case Instruction::FPExt:
5066 CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true);
5067 break;
5068 case Instruction::FPToUI:
5069 CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true);
5070 break;
5071 case Instruction::FPToSI:
5072 CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true);
5073 break;
5074 case Instruction::UIToFP:
5075 CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true);
5076 break;
5077 case Instruction::SIToFP:
5078 CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true);
5079 break;
5080 default:
5081 break;
5082 }
David Majnemerd2a074b2016-04-29 18:40:34 +00005083
5084 if (!CastedTo)
5085 return nullptr;
5086
David Majnemerd2a074b2016-04-29 18:40:34 +00005087 // Make sure the cast doesn't lose any information.
Sanjay Patel14a4b812017-01-29 16:34:57 +00005088 Constant *CastedBack =
5089 ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true);
David Majnemerd2a074b2016-04-29 18:40:34 +00005090 if (CastedBack != C)
5091 return nullptr;
5092
5093 return CastedTo;
James Molloy270ef8c2015-05-15 16:04:50 +00005094}
5095
Sanjay Patele8dc0902016-05-23 17:57:54 +00005096SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
Sanjay Patel1d91ec32018-01-24 15:20:37 +00005097 Instruction::CastOps *CastOp,
5098 unsigned Depth) {
5099 if (Depth >= MaxDepth)
5100 return {SPF_UNKNOWN, SPNB_NA, false};
5101
James Molloy270ef8c2015-05-15 16:04:50 +00005102 SelectInst *SI = dyn_cast<SelectInst>(V);
James Molloy134bec22015-08-11 09:12:57 +00005103 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
James Molloy270ef8c2015-05-15 16:04:50 +00005104
James Molloy134bec22015-08-11 09:12:57 +00005105 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
5106 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
James Molloy270ef8c2015-05-15 16:04:50 +00005107
James Molloy134bec22015-08-11 09:12:57 +00005108 CmpInst::Predicate Pred = CmpI->getPredicate();
James Molloy270ef8c2015-05-15 16:04:50 +00005109 Value *CmpLHS = CmpI->getOperand(0);
5110 Value *CmpRHS = CmpI->getOperand(1);
5111 Value *TrueVal = SI->getTrueValue();
5112 Value *FalseVal = SI->getFalseValue();
James Molloy134bec22015-08-11 09:12:57 +00005113 FastMathFlags FMF;
5114 if (isa<FPMathOperator>(CmpI))
5115 FMF = CmpI->getFastMathFlags();
James Molloy270ef8c2015-05-15 16:04:50 +00005116
5117 // Bail out early.
5118 if (CmpI->isEquality())
James Molloy134bec22015-08-11 09:12:57 +00005119 return {SPF_UNKNOWN, SPNB_NA, false};
James Molloy270ef8c2015-05-15 16:04:50 +00005120
5121 // Deal with type mismatches.
5122 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
Sanjay Patel9a399792017-12-26 15:09:19 +00005123 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
5124 // If this is a potential fmin/fmax with a cast to integer, then ignore
5125 // -0.0 because there is no corresponding integer value.
5126 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
5127 FMF.setNoSignedZeros();
James Molloy134bec22015-08-11 09:12:57 +00005128 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
James Molloy270ef8c2015-05-15 16:04:50 +00005129 cast<CastInst>(TrueVal)->getOperand(0), C,
Sanjay Patel1d91ec32018-01-24 15:20:37 +00005130 LHS, RHS, Depth);
Sanjay Patel9a399792017-12-26 15:09:19 +00005131 }
5132 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
5133 // If this is a potential fmin/fmax with a cast to integer, then ignore
5134 // -0.0 because there is no corresponding integer value.
5135 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
5136 FMF.setNoSignedZeros();
James Molloy134bec22015-08-11 09:12:57 +00005137 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
James Molloy270ef8c2015-05-15 16:04:50 +00005138 C, cast<CastInst>(FalseVal)->getOperand(0),
Sanjay Patel1d91ec32018-01-24 15:20:37 +00005139 LHS, RHS, Depth);
Sanjay Patel9a399792017-12-26 15:09:19 +00005140 }
James Molloy270ef8c2015-05-15 16:04:50 +00005141 }
James Molloy134bec22015-08-11 09:12:57 +00005142 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
Sanjay Patel1d91ec32018-01-24 15:20:37 +00005143 LHS, RHS, Depth);
James Molloy270ef8c2015-05-15 16:04:50 +00005144}
Sanjoy Dasa7e13782015-10-24 05:37:35 +00005145
Sanjay Patel7ed0bc22018-03-06 16:57:55 +00005146CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) {
5147 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
5148 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
5149 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
5150 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
5151 if (SPF == SPF_FMINNUM)
5152 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
5153 if (SPF == SPF_FMAXNUM)
5154 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
5155 llvm_unreachable("unhandled!");
5156}
5157
5158SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) {
5159 if (SPF == SPF_SMIN) return SPF_SMAX;
5160 if (SPF == SPF_UMIN) return SPF_UMAX;
5161 if (SPF == SPF_SMAX) return SPF_SMIN;
5162 if (SPF == SPF_UMAX) return SPF_UMIN;
5163 llvm_unreachable("unhandled!");
5164}
5165
5166CmpInst::Predicate llvm::getInverseMinMaxPred(SelectPatternFlavor SPF) {
5167 return getMinMaxPred(getInverseMinMaxFlavor(SPF));
5168}
5169
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005170/// Return true if "icmp Pred LHS RHS" is always true.
Chad Rosiere42b44b2017-07-28 14:39:06 +00005171static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
5172 const Value *RHS, const DataLayout &DL,
5173 unsigned Depth) {
Sanjoy Dasaf1400f2015-11-10 23:56:15 +00005174 assert(!LHS->getType()->isVectorTy() && "TODO: extend to handle vectors!");
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005175 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
5176 return true;
5177
5178 switch (Pred) {
5179 default:
5180 return false;
5181
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005182 case CmpInst::ICMP_SLE: {
Sanjoy Dasaf1400f2015-11-10 23:56:15 +00005183 const APInt *C;
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005184
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005185 // LHS s<= LHS +_{nsw} C if C >= 0
Sanjoy Dasdc26df42015-11-11 00:16:41 +00005186 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))))
Sanjoy Dasaf1400f2015-11-10 23:56:15 +00005187 return !C->isNegative();
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005188 return false;
5189 }
5190
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005191 case CmpInst::ICMP_ULE: {
Sanjoy Dasaf1400f2015-11-10 23:56:15 +00005192 const APInt *C;
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005193
Sanjoy Dasdc26df42015-11-11 00:16:41 +00005194 // LHS u<= LHS +_{nuw} C for any C
5195 if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C))))
Sanjoy Dasc01b4d22015-11-06 19:01:03 +00005196 return true;
Sanjoy Das92568102015-11-10 23:56:20 +00005197
5198 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
Pete Cooper35b00d52016-08-13 01:05:32 +00005199 auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B,
5200 const Value *&X,
Sanjoy Das92568102015-11-10 23:56:20 +00005201 const APInt *&CA, const APInt *&CB) {
5202 if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) &&
5203 match(B, m_NUWAdd(m_Specific(X), m_APInt(CB))))
5204 return true;
5205
5206 // If X & C == 0 then (X | C) == X +_{nuw} C
5207 if (match(A, m_Or(m_Value(X), m_APInt(CA))) &&
5208 match(B, m_Or(m_Specific(X), m_APInt(CB)))) {
Craig Topperb45eabc2017-04-26 16:39:58 +00005209 KnownBits Known(CA->getBitWidth());
Chad Rosiere42b44b2017-07-28 14:39:06 +00005210 computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr,
5211 /*CxtI*/ nullptr, /*DT*/ nullptr);
Craig Topperb45eabc2017-04-26 16:39:58 +00005212 if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero))
Sanjoy Das92568102015-11-10 23:56:20 +00005213 return true;
5214 }
5215
5216 return false;
5217 };
5218
Pete Cooper35b00d52016-08-13 01:05:32 +00005219 const Value *X;
Sanjoy Das92568102015-11-10 23:56:20 +00005220 const APInt *CLHS, *CRHS;
Sanjoy Dasdc26df42015-11-11 00:16:41 +00005221 if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS))
5222 return CLHS->ule(*CRHS);
Sanjoy Das92568102015-11-10 23:56:20 +00005223
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005224 return false;
5225 }
5226 }
5227}
5228
5229/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
Chad Rosier41dd31f2016-04-20 19:15:26 +00005230/// ALHS ARHS" is true. Otherwise, return None.
5231static Optional<bool>
Pete Cooper35b00d52016-08-13 01:05:32 +00005232isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
Chad Rosiere42b44b2017-07-28 14:39:06 +00005233 const Value *ARHS, const Value *BLHS, const Value *BRHS,
5234 const DataLayout &DL, unsigned Depth) {
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005235 switch (Pred) {
5236 default:
Chad Rosier41dd31f2016-04-20 19:15:26 +00005237 return None;
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005238
5239 case CmpInst::ICMP_SLT:
5240 case CmpInst::ICMP_SLE:
Chad Rosiere42b44b2017-07-28 14:39:06 +00005241 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) &&
5242 isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth))
Chad Rosier41dd31f2016-04-20 19:15:26 +00005243 return true;
5244 return None;
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005245
5246 case CmpInst::ICMP_ULT:
5247 case CmpInst::ICMP_ULE:
Chad Rosiere42b44b2017-07-28 14:39:06 +00005248 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) &&
5249 isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth))
Chad Rosier41dd31f2016-04-20 19:15:26 +00005250 return true;
5251 return None;
Sanjoy Das9349dcc2015-11-06 19:00:57 +00005252 }
5253}
5254
Chad Rosier226a7342016-05-05 17:41:19 +00005255/// Return true if the operands of the two compares match. IsSwappedOps is true
5256/// when the operands match, but are swapped.
Pete Cooper35b00d52016-08-13 01:05:32 +00005257static bool isMatchingOps(const Value *ALHS, const Value *ARHS,
5258 const Value *BLHS, const Value *BRHS,
Chad Rosier226a7342016-05-05 17:41:19 +00005259 bool &IsSwappedOps) {
5260
5261 bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS);
5262 IsSwappedOps = (ALHS == BRHS && ARHS == BLHS);
5263 return IsMatchingOps || IsSwappedOps;
5264}
5265
Sanjay Patel798c5982018-12-19 16:49:18 +00005266/// Return true if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is true.
5267/// Return false if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is false.
5268/// Otherwise, return None if we can't infer anything.
Chad Rosier41dd31f2016-04-20 19:15:26 +00005269static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred,
Chad Rosier41dd31f2016-04-20 19:15:26 +00005270 CmpInst::Predicate BPred,
Sanjay Patel798c5982018-12-19 16:49:18 +00005271 bool AreSwappedOps) {
5272 // Canonicalize the predicate as if the operands were not commuted.
5273 if (AreSwappedOps)
Chad Rosierb7dfbb42016-04-19 17:19:14 +00005274 BPred = ICmpInst::getSwappedPredicate(BPred);
Sanjay Patel798c5982018-12-19 16:49:18 +00005275
Chad Rosier99bc4802016-04-21 16:18:02 +00005276 if (CmpInst::isImpliedTrueByMatchingCmp(APred, BPred))
Chad Rosierb7dfbb42016-04-19 17:19:14 +00005277 return true;
Chad Rosier99bc4802016-04-21 16:18:02 +00005278 if (CmpInst::isImpliedFalseByMatchingCmp(APred, BPred))
Chad Rosier41dd31f2016-04-20 19:15:26 +00005279 return false;
Chad Rosierb7dfbb42016-04-19 17:19:14 +00005280
Chad Rosier41dd31f2016-04-20 19:15:26 +00005281 return None;
Chad Rosierb7dfbb42016-04-19 17:19:14 +00005282}
5283
Sanjay Patel798c5982018-12-19 16:49:18 +00005284/// Return true if "icmp APred X, C1" implies "icmp BPred X, C2" is true.
5285/// Return false if "icmp APred X, C1" implies "icmp BPred X, C2" is false.
5286/// Otherwise, return None if we can't infer anything.
Chad Rosier25cfb7d2016-05-05 15:39:18 +00005287static Optional<bool>
Sanjay Patel798c5982018-12-19 16:49:18 +00005288isImpliedCondMatchingImmOperands(CmpInst::Predicate APred,
Pete Cooper35b00d52016-08-13 01:05:32 +00005289 const ConstantInt *C1,
5290 CmpInst::Predicate BPred,
Sanjay Patel798c5982018-12-19 16:49:18 +00005291 const ConstantInt *C2) {
Chad Rosier25cfb7d2016-05-05 15:39:18 +00005292 ConstantRange DomCR =
5293 ConstantRange::makeExactICmpRegion(APred, C1->getValue());
5294 ConstantRange CR =
5295 ConstantRange::makeAllowedICmpRegion(BPred, C2->getValue());
5296 ConstantRange Intersection = DomCR.intersectWith(CR);
5297 ConstantRange Difference = DomCR.difference(CR);
5298 if (Intersection.isEmptySet())
5299 return false;
5300 if (Difference.isEmptySet())
5301 return true;
5302 return None;
5303}
5304
Chad Rosier2f498032017-07-28 18:47:43 +00005305/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
5306/// false. Otherwise, return None if we can't infer anything.
5307static Optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
5308 const ICmpInst *RHS,
Chad Rosierdfd1de62017-08-01 20:18:54 +00005309 const DataLayout &DL, bool LHSIsTrue,
Chad Rosier2f498032017-07-28 18:47:43 +00005310 unsigned Depth) {
5311 Value *ALHS = LHS->getOperand(0);
5312 Value *ARHS = LHS->getOperand(1);
Chad Rosiera72a9ff2017-07-06 20:00:25 +00005313 // The rest of the logic assumes the LHS condition is true. If that's not the
5314 // case, invert the predicate to make it so.
Chad Rosier2f498032017-07-28 18:47:43 +00005315 ICmpInst::Predicate APred =
Chad Rosierdfd1de62017-08-01 20:18:54 +00005316 LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
Chad Rosier2f498032017-07-28 18:47:43 +00005317
5318 Value *BLHS = RHS->getOperand(0);
5319 Value *BRHS = RHS->getOperand(1);
5320 ICmpInst::Predicate BPred = RHS->getPredicate();
Chad Rosiere2cbd132016-04-25 17:23:36 +00005321
Chad Rosier226a7342016-05-05 17:41:19 +00005322 // Can we infer anything when the two compares have matching operands?
Sanjay Patel798c5982018-12-19 16:49:18 +00005323 bool AreSwappedOps;
5324 if (isMatchingOps(ALHS, ARHS, BLHS, BRHS, AreSwappedOps)) {
Chad Rosier226a7342016-05-05 17:41:19 +00005325 if (Optional<bool> Implication = isImpliedCondMatchingOperands(
Sanjay Patel798c5982018-12-19 16:49:18 +00005326 APred, BPred, AreSwappedOps))
Chad Rosier25cfb7d2016-05-05 15:39:18 +00005327 return Implication;
Chad Rosier226a7342016-05-05 17:41:19 +00005328 // No amount of additional analysis will infer the second condition, so
5329 // early exit.
5330 return None;
5331 }
5332
5333 // Can we infer anything when the LHS operands match and the RHS operands are
5334 // constants (not necessarily matching)?
5335 if (ALHS == BLHS && isa<ConstantInt>(ARHS) && isa<ConstantInt>(BRHS)) {
5336 if (Optional<bool> Implication = isImpliedCondMatchingImmOperands(
Sanjay Patel798c5982018-12-19 16:49:18 +00005337 APred, cast<ConstantInt>(ARHS), BPred, cast<ConstantInt>(BRHS)))
Chad Rosier226a7342016-05-05 17:41:19 +00005338 return Implication;
5339 // No amount of additional analysis will infer the second condition, so
5340 // early exit.
5341 return None;
Chad Rosier25cfb7d2016-05-05 15:39:18 +00005342 }
5343
Chad Rosier41dd31f2016-04-20 19:15:26 +00005344 if (APred == BPred)
Chad Rosiere42b44b2017-07-28 14:39:06 +00005345 return isImpliedCondOperands(APred, ALHS, ARHS, BLHS, BRHS, DL, Depth);
Chad Rosier41dd31f2016-04-20 19:15:26 +00005346 return None;
Sanjoy Das3ef1e682015-10-28 03:20:19 +00005347}
Chad Rosier2f498032017-07-28 18:47:43 +00005348
Chad Rosierf73a10d2017-08-01 19:22:36 +00005349/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
5350/// false. Otherwise, return None if we can't infer anything. We expect the
5351/// RHS to be an icmp and the LHS to be an 'and' or an 'or' instruction.
5352static Optional<bool> isImpliedCondAndOr(const BinaryOperator *LHS,
5353 const ICmpInst *RHS,
Chad Rosierdfd1de62017-08-01 20:18:54 +00005354 const DataLayout &DL, bool LHSIsTrue,
Chad Rosierf73a10d2017-08-01 19:22:36 +00005355 unsigned Depth) {
5356 // The LHS must be an 'or' or an 'and' instruction.
5357 assert((LHS->getOpcode() == Instruction::And ||
5358 LHS->getOpcode() == Instruction::Or) &&
5359 "Expected LHS to be 'and' or 'or'.");
5360
Davide Italiano1a943a92017-08-09 16:06:54 +00005361 assert(Depth <= MaxDepth && "Hit recursion limit");
Chad Rosierf73a10d2017-08-01 19:22:36 +00005362
5363 // If the result of an 'or' is false, then we know both legs of the 'or' are
5364 // false. Similarly, if the result of an 'and' is true, then we know both
5365 // legs of the 'and' are true.
5366 Value *ALHS, *ARHS;
Chad Rosierdfd1de62017-08-01 20:18:54 +00005367 if ((!LHSIsTrue && match(LHS, m_Or(m_Value(ALHS), m_Value(ARHS)))) ||
5368 (LHSIsTrue && match(LHS, m_And(m_Value(ALHS), m_Value(ARHS))))) {
Chad Rosierf73a10d2017-08-01 19:22:36 +00005369 // FIXME: Make this non-recursion.
5370 if (Optional<bool> Implication =
Chad Rosierdfd1de62017-08-01 20:18:54 +00005371 isImpliedCondition(ALHS, RHS, DL, LHSIsTrue, Depth + 1))
Chad Rosierf73a10d2017-08-01 19:22:36 +00005372 return Implication;
5373 if (Optional<bool> Implication =
Chad Rosierdfd1de62017-08-01 20:18:54 +00005374 isImpliedCondition(ARHS, RHS, DL, LHSIsTrue, Depth + 1))
Chad Rosierf73a10d2017-08-01 19:22:36 +00005375 return Implication;
5376 return None;
5377 }
5378 return None;
5379}
5380
Chad Rosier2f498032017-07-28 18:47:43 +00005381Optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
Chad Rosierdfd1de62017-08-01 20:18:54 +00005382 const DataLayout &DL, bool LHSIsTrue,
Chad Rosier2f498032017-07-28 18:47:43 +00005383 unsigned Depth) {
Davide Italiano30e51942017-08-09 15:13:50 +00005384 // Bail out when we hit the limit.
5385 if (Depth == MaxDepth)
5386 return None;
5387
Chad Rosierf73a10d2017-08-01 19:22:36 +00005388 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
5389 // example.
Chad Rosier2f498032017-07-28 18:47:43 +00005390 if (LHS->getType() != RHS->getType())
5391 return None;
5392
5393 Type *OpTy = LHS->getType();
Chad Rosierf73a10d2017-08-01 19:22:36 +00005394 assert(OpTy->isIntOrIntVectorTy(1) && "Expected integer type only!");
Chad Rosier2f498032017-07-28 18:47:43 +00005395
5396 // LHS ==> RHS by definition
5397 if (LHS == RHS)
Chad Rosierdfd1de62017-08-01 20:18:54 +00005398 return LHSIsTrue;
Chad Rosier2f498032017-07-28 18:47:43 +00005399
Chad Rosierf73a10d2017-08-01 19:22:36 +00005400 // FIXME: Extending the code below to handle vectors.
Chad Rosier2f498032017-07-28 18:47:43 +00005401 if (OpTy->isVectorTy())
Chad Rosier2f498032017-07-28 18:47:43 +00005402 return None;
Chad Rosierf73a10d2017-08-01 19:22:36 +00005403
Chad Rosier2f498032017-07-28 18:47:43 +00005404 assert(OpTy->isIntegerTy(1) && "implied by above");
5405
Chad Rosier2f498032017-07-28 18:47:43 +00005406 // Both LHS and RHS are icmps.
Chad Rosierf73a10d2017-08-01 19:22:36 +00005407 const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
5408 const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS);
5409 if (LHSCmp && RHSCmp)
Chad Rosierdfd1de62017-08-01 20:18:54 +00005410 return isImpliedCondICmps(LHSCmp, RHSCmp, DL, LHSIsTrue, Depth);
Chad Rosier2f498032017-07-28 18:47:43 +00005411
Chad Rosierf73a10d2017-08-01 19:22:36 +00005412 // The LHS should be an 'or' or an 'and' instruction. We expect the RHS to be
5413 // an icmp. FIXME: Add support for and/or on the RHS.
5414 const BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHS);
5415 if (LHSBO && RHSCmp) {
5416 if ((LHSBO->getOpcode() == Instruction::And ||
5417 LHSBO->getOpcode() == Instruction::Or))
Chad Rosierdfd1de62017-08-01 20:18:54 +00005418 return isImpliedCondAndOr(LHSBO, RHSCmp, DL, LHSIsTrue, Depth);
Chad Rosier2f498032017-07-28 18:47:43 +00005419 }
Chad Rosierf73a10d2017-08-01 19:22:36 +00005420 return None;
Chad Rosier2f498032017-07-28 18:47:43 +00005421}
Sanjay Patel7d82d372018-12-02 13:26:03 +00005422
5423Optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
5424 const Instruction *ContextI,
5425 const DataLayout &DL) {
5426 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
5427 if (!ContextI || !ContextI->getParent())
5428 return None;
5429
5430 // TODO: This is a poor/cheap way to determine dominance. Should we use a
5431 // dominator tree (eg, from a SimplifyQuery) instead?
5432 const BasicBlock *ContextBB = ContextI->getParent();
5433 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
5434 if (!PredBB)
5435 return None;
5436
5437 // We need a conditional branch in the predecessor.
5438 Value *PredCond;
5439 BasicBlock *TrueBB, *FalseBB;
5440 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
5441 return None;
5442
5443 // The branch should get simplified. Don't bother simplifying this condition.
5444 if (TrueBB == FalseBB)
5445 return None;
5446
5447 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
5448 "Predecessor block does not point to successor?");
5449
5450 // Is this condition implied by the predecessor condition?
5451 bool CondIsTrue = TrueBB == ContextBB;
5452 return isImpliedCondition(PredCond, Cond, DL, CondIsTrue);
5453}