Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 1 | //===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass statically checks for common and easily-identified constructs |
| 10 | // which produce undefined or likely unintended behavior in LLVM IR. |
| 11 | // |
| 12 | // It is not a guarantee of correctness, in two ways. First, it isn't |
| 13 | // comprehensive. There are checks which could be done statically which are |
| 14 | // not yet implemented. Some of these are indicated by TODO comments, but |
| 15 | // those aren't comprehensive either. Second, many conditions cannot be |
| 16 | // checked statically. This pass does no dynamic instrumentation, so it |
| 17 | // can't check for all possible problems. |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 18 | // |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 19 | // Another limitation is that it assumes all code will be executed. A store |
| 20 | // through a null pointer in a basic block which is never reached is harmless, |
Dan Gohman | f855b39 | 2010-07-06 15:21:57 +0000 | [diff] [blame] | 21 | // but this pass will warn about it anyway. This is the main reason why most |
| 22 | // of these checks live here instead of in the Verifier pass. |
Dan Gohman | c951e6e | 2010-04-22 01:30:05 +0000 | [diff] [blame] | 23 | // |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 24 | // Optimization passes may make conditions that this pass checks for more or |
| 25 | // less obvious. If an optimization pass appears to be introducing a warning, |
| 26 | // it may be that the optimization pass is merely exposing an existing |
| 27 | // condition in the code. |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 28 | // |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 29 | // This code may be run before instcombine. In many cases, instcombine checks |
| 30 | // for the same kinds of things and turns instructions with undefined behavior |
| 31 | // into unreachable (or equivalent). Because of this, this pass makes some |
| 32 | // effort to look through bitcasts and so on. |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 33 | // |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/Lint.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/APInt.h" |
| 38 | #include "llvm/ADT/ArrayRef.h" |
| 39 | #include "llvm/ADT/SmallPtrSet.h" |
| 40 | #include "llvm/ADT/Twine.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/AssumptionCache.h" |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/ConstantFolding.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/InstructionSimplify.h" |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/Loads.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/MemoryLocation.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 50 | #include "llvm/IR/Argument.h" |
| 51 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 52 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 53 | #include "llvm/IR/Constant.h" |
| 54 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 55 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 56 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 57 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 58 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 59 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 60 | #include "llvm/IR/InstVisitor.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 61 | #include "llvm/IR/InstrTypes.h" |
| 62 | #include "llvm/IR/Instruction.h" |
| 63 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 64 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 65 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 66 | #include "llvm/IR/Module.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 67 | #include "llvm/IR/Type.h" |
| 68 | #include "llvm/IR/Value.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 69 | #include "llvm/Pass.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 70 | #include "llvm/Support/Casting.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 71 | #include "llvm/Support/Debug.h" |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 72 | #include "llvm/Support/KnownBits.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 73 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 74 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 75 | #include <cassert> |
| 76 | #include <cstdint> |
| 77 | #include <iterator> |
| 78 | #include <string> |
| 79 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 80 | using namespace llvm; |
| 81 | |
| 82 | namespace { |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 83 | namespace MemRef { |
Benjamin Kramer | 57a3d08 | 2015-03-08 16:07:39 +0000 | [diff] [blame] | 84 | static const unsigned Read = 1; |
| 85 | static const unsigned Write = 2; |
| 86 | static const unsigned Callee = 4; |
| 87 | static const unsigned Branchee = 8; |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 88 | } // end namespace MemRef |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 89 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 90 | class Lint : public FunctionPass, public InstVisitor<Lint> { |
| 91 | friend class InstVisitor<Lint>; |
| 92 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 93 | void visitFunction(Function &F); |
| 94 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 95 | void visitCallSite(CallSite CS); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 96 | void visitMemoryReference(Instruction &I, Value *Ptr, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 97 | uint64_t Size, unsigned Align, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 98 | Type *Ty, unsigned Flags); |
Andrew Kaylor | 78b53db | 2015-02-10 19:52:43 +0000 | [diff] [blame] | 99 | void visitEHBeginCatch(IntrinsicInst *II); |
| 100 | void visitEHEndCatch(IntrinsicInst *II); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 101 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 102 | void visitCallInst(CallInst &I); |
| 103 | void visitInvokeInst(InvokeInst &I); |
| 104 | void visitReturnInst(ReturnInst &I); |
| 105 | void visitLoadInst(LoadInst &I); |
| 106 | void visitStoreInst(StoreInst &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 107 | void visitXor(BinaryOperator &I); |
| 108 | void visitSub(BinaryOperator &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 109 | void visitLShr(BinaryOperator &I); |
| 110 | void visitAShr(BinaryOperator &I); |
| 111 | void visitShl(BinaryOperator &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 112 | void visitSDiv(BinaryOperator &I); |
| 113 | void visitUDiv(BinaryOperator &I); |
| 114 | void visitSRem(BinaryOperator &I); |
| 115 | void visitURem(BinaryOperator &I); |
| 116 | void visitAllocaInst(AllocaInst &I); |
| 117 | void visitVAArgInst(VAArgInst &I); |
| 118 | void visitIndirectBrInst(IndirectBrInst &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 119 | void visitExtractElementInst(ExtractElementInst &I); |
| 120 | void visitInsertElementInst(InsertElementInst &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 121 | void visitUnreachableInst(UnreachableInst &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 122 | |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 123 | Value *findValue(Value *V, bool OffsetOk) const; |
| 124 | Value *findValueImpl(Value *V, bool OffsetOk, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 125 | SmallPtrSetImpl<Value *> &Visited) const; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 126 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 127 | public: |
| 128 | Module *Mod; |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 129 | const DataLayout *DL; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 130 | AliasAnalysis *AA; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 131 | AssumptionCache *AC; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 132 | DominatorTree *DT; |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 133 | TargetLibraryInfo *TLI; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 134 | |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 135 | std::string Messages; |
| 136 | raw_string_ostream MessagesStr; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 137 | |
| 138 | static char ID; // Pass identification, replacement for typeid |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 139 | Lint() : FunctionPass(ID), MessagesStr(Messages) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 140 | initializeLintPass(*PassRegistry::getPassRegistry()); |
| 141 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 142 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 143 | bool runOnFunction(Function &F) override; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 144 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 145 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 146 | AU.setPreservesAll(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 147 | AU.addRequired<AAResultsWrapperPass>(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 148 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 149 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 150 | AU.addRequired<DominatorTreeWrapperPass>(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 151 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 152 | void print(raw_ostream &O, const Module *M) const override {} |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 153 | |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 154 | void WriteValues(ArrayRef<const Value *> Vs) { |
| 155 | for (const Value *V : Vs) { |
| 156 | if (!V) |
| 157 | continue; |
| 158 | if (isa<Instruction>(V)) { |
| 159 | MessagesStr << *V << '\n'; |
| 160 | } else { |
| 161 | V->printAsOperand(MessagesStr, true, Mod); |
| 162 | MessagesStr << '\n'; |
| 163 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 167 | /// A check failed, so printout out the condition and the message. |
Duncan P. N. Exon Smith | f2929c9 | 2015-03-16 17:49:03 +0000 | [diff] [blame] | 168 | /// |
| 169 | /// This provides a nice place to put a breakpoint if you want to see why |
| 170 | /// something is not correct. |
Duncan P. N. Exon Smith | ec9d3f7 | 2015-03-14 16:47:37 +0000 | [diff] [blame] | 171 | void CheckFailed(const Twine &Message) { MessagesStr << Message << '\n'; } |
| 172 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 173 | /// A check failed (with values to print). |
Duncan P. N. Exon Smith | f2929c9 | 2015-03-16 17:49:03 +0000 | [diff] [blame] | 174 | /// |
| 175 | /// This calls the Message-only version so that the above is easier to set |
| 176 | /// a breakpoint on. |
Duncan P. N. Exon Smith | ec9d3f7 | 2015-03-14 16:47:37 +0000 | [diff] [blame] | 177 | template <typename T1, typename... Ts> |
| 178 | void CheckFailed(const Twine &Message, const T1 &V1, const Ts &...Vs) { |
| 179 | CheckFailed(Message); |
| 180 | WriteValues({V1, Vs...}); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 181 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 182 | }; |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 183 | } // end anonymous namespace |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 184 | |
| 185 | char Lint::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 186 | INITIALIZE_PASS_BEGIN(Lint, "lint", "Statically lint-checks LLVM IR", |
| 187 | false, true) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 188 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 189 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 190 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 191 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 192 | INITIALIZE_PASS_END(Lint, "lint", "Statically lint-checks LLVM IR", |
| 193 | false, true) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 194 | |
| 195 | // Assert - We know that cond should be true, if not print an error message. |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 196 | #define Assert(C, ...) \ |
Eugene Zelenko | 3e3a057 | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 197 | do { if (!(C)) { CheckFailed(__VA_ARGS__); return; } } while (false) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 198 | |
| 199 | // Lint::run - This is the main Analysis entry point for a |
| 200 | // function. |
| 201 | // |
| 202 | bool Lint::runOnFunction(Function &F) { |
| 203 | Mod = F.getParent(); |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 204 | DL = &F.getParent()->getDataLayout(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 205 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 206 | AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 207 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 208 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 209 | visit(F); |
| 210 | dbgs() << MessagesStr.str(); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 211 | Messages.clear(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 215 | void Lint::visitFunction(Function &F) { |
| 216 | // This isn't undefined behavior, it's just a little unusual, and it's a |
| 217 | // fairly common mistake to neglect to name a function. |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 218 | Assert(F.hasName() || F.hasLocalLinkage(), |
| 219 | "Unusual: Unnamed function with non-local linkage", &F); |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 220 | |
| 221 | // TODO: Check for irreducible control flow. |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void Lint::visitCallSite(CallSite CS) { |
| 225 | Instruction &I = *CS.getInstruction(); |
| 226 | Value *Callee = CS.getCalledValue(); |
| 227 | |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 228 | visitMemoryReference(I, Callee, MemoryLocation::UnknownSize, 0, nullptr, |
| 229 | MemRef::Callee); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 230 | |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 231 | if (Function *F = dyn_cast<Function>(findValue(Callee, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 232 | /*OffsetOk=*/false))) { |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 233 | Assert(CS.getCallingConv() == F->getCallingConv(), |
| 234 | "Undefined behavior: Caller and callee calling convention differ", |
| 235 | &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 237 | FunctionType *FT = F->getFunctionType(); |
Matt Arsenault | b12f2f3 | 2013-11-10 03:18:50 +0000 | [diff] [blame] | 238 | unsigned NumActualArgs = CS.arg_size(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 239 | |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 240 | Assert(FT->isVarArg() ? FT->getNumParams() <= NumActualArgs |
| 241 | : FT->getNumParams() == NumActualArgs, |
| 242 | "Undefined behavior: Call argument count mismatches callee " |
| 243 | "argument count", |
| 244 | &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 245 | |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 246 | Assert(FT->getReturnType() == I.getType(), |
| 247 | "Undefined behavior: Call return type mismatches " |
| 248 | "callee return type", |
| 249 | &I); |
Dan Gohman | c128e70 | 2010-07-12 18:02:04 +0000 | [diff] [blame] | 250 | |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 251 | // Check argument types (in case the callee was casted) and attributes. |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 252 | // TODO: Verify that caller and callee attributes are compatible. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 253 | Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end(); |
| 254 | CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 255 | for (; AI != AE; ++AI) { |
| 256 | Value *Actual = *AI; |
| 257 | if (PI != PE) { |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 258 | Argument *Formal = &*PI++; |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 259 | Assert(Formal->getType() == Actual->getType(), |
| 260 | "Undefined behavior: Call argument type mismatches " |
| 261 | "callee parameter type", |
| 262 | &I); |
Dan Gohman | 49a372c | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 263 | |
Dan Gohman | 3cb55a1 | 2010-12-13 22:53:18 +0000 | [diff] [blame] | 264 | // Check that noalias arguments don't alias other arguments. This is |
| 265 | // not fully precise because we don't know the sizes of the dereferenced |
| 266 | // memory regions. |
Mikael Holmen | fb2fd20 | 2017-12-27 08:48:33 +0000 | [diff] [blame] | 267 | if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy()) { |
| 268 | AttributeList PAL = CS.getAttributes(); |
| 269 | unsigned ArgNo = 0; |
| 270 | for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI) { |
| 271 | // Skip ByVal arguments since they will be memcpy'd to the callee's |
| 272 | // stack so we're not really passing the pointer anyway. |
| 273 | if (PAL.hasParamAttribute(ArgNo++, Attribute::ByVal)) |
| 274 | continue; |
Dan Gohman | 201acdb | 2010-12-10 20:04:06 +0000 | [diff] [blame] | 275 | if (AI != BI && (*BI)->getType()->isPointerTy()) { |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 276 | AliasResult Result = AA->alias(*AI, *BI); |
| 277 | Assert(Result != MustAlias && Result != PartialAlias, |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 278 | "Unusual: noalias argument aliases another argument", &I); |
Dan Gohman | 201acdb | 2010-12-10 20:04:06 +0000 | [diff] [blame] | 279 | } |
Mikael Holmen | fb2fd20 | 2017-12-27 08:48:33 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
Dan Gohman | 49a372c | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 282 | |
| 283 | // Check that an sret argument points to valid memory. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 284 | if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 285 | Type *Ty = |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 286 | cast<PointerType>(Formal->getType())->getElementType(); |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 287 | visitMemoryReference(I, Actual, DL->getTypeStoreSize(Ty), |
| 288 | DL->getABITypeAlignment(Ty), Ty, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 289 | MemRef::Read | MemRef::Write); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Mikael Holmen | 6e60297 | 2017-11-15 07:46:48 +0000 | [diff] [blame] | 295 | if (CS.isCall()) { |
| 296 | const CallInst *CI = cast<CallInst>(CS.getInstruction()); |
| 297 | if (CI->isTailCall()) { |
| 298 | const AttributeList &PAL = CI->getAttributes(); |
| 299 | unsigned ArgNo = 0; |
| 300 | for (Value *Arg : CS.args()) { |
| 301 | // Skip ByVal arguments since they will be memcpy'd to the callee's |
| 302 | // stack anyway. |
| 303 | if (PAL.hasParamAttribute(ArgNo++, Attribute::ByVal)) |
| 304 | continue; |
| 305 | Value *Obj = findValue(Arg, /*OffsetOk=*/true); |
| 306 | Assert(!isa<AllocaInst>(Obj), |
| 307 | "Undefined behavior: Call with \"tail\" keyword references " |
| 308 | "alloca", |
| 309 | &I); |
| 310 | } |
Dan Gohman | 1249adf | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 311 | } |
Mikael Holmen | 6e60297 | 2017-11-15 07:46:48 +0000 | [diff] [blame] | 312 | } |
Dan Gohman | 1249adf | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 313 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 314 | |
| 315 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) |
| 316 | switch (II->getIntrinsicID()) { |
| 317 | default: break; |
| 318 | |
| 319 | // TODO: Check more intrinsics |
| 320 | |
| 321 | case Intrinsic::memcpy: { |
| 322 | MemCpyInst *MCI = cast<MemCpyInst>(&I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 323 | // TODO: If the size is known, use it. |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 324 | visitMemoryReference(I, MCI->getDest(), MemoryLocation::UnknownSize, |
Daniel Neilson | 147810d | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 325 | MCI->getDestAlignment(), nullptr, MemRef::Write); |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 326 | visitMemoryReference(I, MCI->getSource(), MemoryLocation::UnknownSize, |
Daniel Neilson | 147810d | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 327 | MCI->getSourceAlignment(), nullptr, MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 328 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 329 | // Check that the memcpy arguments don't overlap. The AliasAnalysis API |
| 330 | // isn't expressive enough for what we really want to do. Known partial |
| 331 | // overlap is not distinguished from the case where nothing is known. |
George Burgess IV | 1329cf1 | 2018-12-23 02:50:08 +0000 | [diff] [blame] | 332 | auto Size = LocationSize::unknown(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 333 | if (const ConstantInt *Len = |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 334 | dyn_cast<ConstantInt>(findValue(MCI->getLength(), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 335 | /*OffsetOk=*/false))) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 336 | if (Len->getValue().isIntN(32)) |
George Burgess IV | 1329cf1 | 2018-12-23 02:50:08 +0000 | [diff] [blame] | 337 | Size = LocationSize::precise(Len->getValue().getZExtValue()); |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 338 | Assert(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) != |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 339 | MustAlias, |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 340 | "Undefined behavior: memcpy source and destination overlap", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | case Intrinsic::memmove: { |
| 344 | MemMoveInst *MMI = cast<MemMoveInst>(&I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 345 | // TODO: If the size is known, use it. |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 346 | visitMemoryReference(I, MMI->getDest(), MemoryLocation::UnknownSize, |
Daniel Neilson | 147810d | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 347 | MMI->getDestAlignment(), nullptr, MemRef::Write); |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 348 | visitMemoryReference(I, MMI->getSource(), MemoryLocation::UnknownSize, |
Daniel Neilson | 147810d | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 349 | MMI->getSourceAlignment(), nullptr, MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | case Intrinsic::memset: { |
| 353 | MemSetInst *MSI = cast<MemSetInst>(&I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 354 | // TODO: If the size is known, use it. |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 355 | visitMemoryReference(I, MSI->getDest(), MemoryLocation::UnknownSize, |
Daniel Neilson | 147810d | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 356 | MSI->getDestAlignment(), nullptr, MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 357 | break; |
| 358 | } |
| 359 | |
| 360 | case Intrinsic::vastart: |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 361 | Assert(I.getParent()->getParent()->isVarArg(), |
| 362 | "Undefined behavior: va_start called in a non-varargs function", |
| 363 | &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 364 | |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 365 | visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0, |
| 366 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 367 | break; |
| 368 | case Intrinsic::vacopy: |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 369 | visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0, |
| 370 | nullptr, MemRef::Write); |
| 371 | visitMemoryReference(I, CS.getArgument(1), MemoryLocation::UnknownSize, 0, |
| 372 | nullptr, MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 373 | break; |
| 374 | case Intrinsic::vaend: |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 375 | visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0, |
| 376 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 377 | break; |
Dan Gohman | a20a5cd | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 378 | |
| 379 | case Intrinsic::stackrestore: |
| 380 | // Stackrestore doesn't read or write memory, but it sets the |
| 381 | // stack pointer, which the compiler may read from or write to |
| 382 | // at any time, so check it for both readability and writeability. |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 383 | visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0, |
| 384 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | a20a5cd | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 385 | break; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | void Lint::visitCallInst(CallInst &I) { |
| 390 | return visitCallSite(&I); |
| 391 | } |
| 392 | |
| 393 | void Lint::visitInvokeInst(InvokeInst &I) { |
| 394 | return visitCallSite(&I); |
| 395 | } |
| 396 | |
| 397 | void Lint::visitReturnInst(ReturnInst &I) { |
| 398 | Function *F = I.getParent()->getParent(); |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 399 | Assert(!F->doesNotReturn(), |
| 400 | "Unusual: Return statement in function with noreturn attribute", &I); |
Dan Gohman | ddba4b7 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 401 | |
| 402 | if (Value *V = I.getReturnValue()) { |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 403 | Value *Obj = findValue(V, /*OffsetOk=*/true); |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 404 | Assert(!isa<AllocaInst>(Obj), "Unusual: Returning alloca value", &I); |
Dan Gohman | ddba4b7 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 405 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 408 | // TODO: Check that the reference is in bounds. |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 409 | // TODO: Check readnone/readonly function attributes. |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 410 | void Lint::visitMemoryReference(Instruction &I, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 411 | Value *Ptr, uint64_t Size, unsigned Align, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 412 | Type *Ty, unsigned Flags) { |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 413 | // If no memory is being referenced, it doesn't matter if the pointer |
| 414 | // is valid. |
| 415 | if (Size == 0) |
| 416 | return; |
| 417 | |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 418 | Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true); |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 419 | Assert(!isa<ConstantPointerNull>(UnderlyingObject), |
| 420 | "Undefined behavior: Null pointer dereference", &I); |
| 421 | Assert(!isa<UndefValue>(UnderlyingObject), |
| 422 | "Undefined behavior: Undef pointer dereference", &I); |
| 423 | Assert(!isa<ConstantInt>(UnderlyingObject) || |
Craig Topper | 79ab643 | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 424 | !cast<ConstantInt>(UnderlyingObject)->isMinusOne(), |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 425 | "Unusual: All-ones pointer dereference", &I); |
| 426 | Assert(!isa<ConstantInt>(UnderlyingObject) || |
| 427 | !cast<ConstantInt>(UnderlyingObject)->isOne(), |
| 428 | "Unusual: Address one pointer dereference", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 429 | |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 430 | if (Flags & MemRef::Write) { |
| 431 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject)) |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 432 | Assert(!GV->isConstant(), "Undefined behavior: Write to read-only memory", |
| 433 | &I); |
| 434 | Assert(!isa<Function>(UnderlyingObject) && |
| 435 | !isa<BlockAddress>(UnderlyingObject), |
| 436 | "Undefined behavior: Write to text section", &I); |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 437 | } |
| 438 | if (Flags & MemRef::Read) { |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 439 | Assert(!isa<Function>(UnderlyingObject), "Unusual: Load from function body", |
| 440 | &I); |
| 441 | Assert(!isa<BlockAddress>(UnderlyingObject), |
| 442 | "Undefined behavior: Load from block address", &I); |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 443 | } |
| 444 | if (Flags & MemRef::Callee) { |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 445 | Assert(!isa<BlockAddress>(UnderlyingObject), |
| 446 | "Undefined behavior: Call to block address", &I); |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 447 | } |
| 448 | if (Flags & MemRef::Branchee) { |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 449 | Assert(!isa<Constant>(UnderlyingObject) || |
| 450 | isa<BlockAddress>(UnderlyingObject), |
| 451 | "Undefined behavior: Branch to non-blockaddress", &I); |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Duncan Sands | a221eea | 2012-09-26 07:45:36 +0000 | [diff] [blame] | 454 | // Check for buffer overflows and misalignment. |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 455 | // Only handles memory references that read/write something simple like an |
| 456 | // alloca instruction or a global variable. |
| 457 | int64_t Offset = 0; |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 458 | if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, *DL)) { |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 459 | // OK, so the access is to a constant offset from Ptr. Check that Ptr is |
| 460 | // something we can handle and if so extract the size of this base object |
| 461 | // along with its alignment. |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 462 | uint64_t BaseSize = MemoryLocation::UnknownSize; |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 463 | unsigned BaseAlign = 0; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 464 | |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 465 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Base)) { |
| 466 | Type *ATy = AI->getAllocatedType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 467 | if (!AI->isArrayAllocation() && ATy->isSized()) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 468 | BaseSize = DL->getTypeAllocSize(ATy); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 469 | BaseAlign = AI->getAlignment(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 470 | if (BaseAlign == 0 && ATy->isSized()) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 471 | BaseAlign = DL->getABITypeAlignment(ATy); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 472 | } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { |
| 473 | // If the global may be defined differently in another compilation unit |
| 474 | // then don't warn about funky memory accesses. |
| 475 | if (GV->hasDefinitiveInitializer()) { |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 476 | Type *GTy = GV->getValueType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 477 | if (GTy->isSized()) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 478 | BaseSize = DL->getTypeAllocSize(GTy); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 479 | BaseAlign = GV->getAlignment(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 480 | if (BaseAlign == 0 && GTy->isSized()) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 481 | BaseAlign = DL->getABITypeAlignment(GTy); |
Duncan Sands | 3f4d0b1 | 2012-09-25 10:00:49 +0000 | [diff] [blame] | 482 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 483 | } |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 484 | |
| 485 | // Accesses from before the start or after the end of the object are not |
| 486 | // defined. |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 487 | Assert(Size == MemoryLocation::UnknownSize || |
| 488 | BaseSize == MemoryLocation::UnknownSize || |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 489 | (Offset >= 0 && Offset + Size <= BaseSize), |
| 490 | "Undefined behavior: Buffer overflow", &I); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 491 | |
| 492 | // Accesses that say that the memory is more aligned than it is are not |
| 493 | // defined. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 494 | if (Align == 0 && Ty && Ty->isSized()) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 495 | Align = DL->getABITypeAlignment(Ty); |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 496 | Assert(!BaseAlign || Align <= MinAlign(BaseAlign, Offset), |
| 497 | "Undefined behavior: Memory reference address is misaligned", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
| 501 | void Lint::visitLoadInst(LoadInst &I) { |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 502 | visitMemoryReference(I, I.getPointerOperand(), |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 503 | DL->getTypeStoreSize(I.getType()), I.getAlignment(), |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 504 | I.getType(), MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | void Lint::visitStoreInst(StoreInst &I) { |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 508 | visitMemoryReference(I, I.getPointerOperand(), |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 509 | DL->getTypeStoreSize(I.getOperand(0)->getType()), |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 510 | I.getAlignment(), |
| 511 | I.getOperand(0)->getType(), MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 514 | void Lint::visitXor(BinaryOperator &I) { |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 515 | Assert(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)), |
| 516 | "Undefined result: xor(undef, undef)", &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | void Lint::visitSub(BinaryOperator &I) { |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 520 | Assert(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)), |
| 521 | "Undefined result: sub(undef, undef)", &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 524 | void Lint::visitLShr(BinaryOperator &I) { |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 525 | if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getOperand(1), |
| 526 | /*OffsetOk=*/false))) |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 527 | Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
| 528 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | void Lint::visitAShr(BinaryOperator &I) { |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 532 | if (ConstantInt *CI = |
| 533 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 534 | Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
| 535 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void Lint::visitShl(BinaryOperator &I) { |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 539 | if (ConstantInt *CI = |
| 540 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 541 | Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
| 542 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 545 | static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, |
| 546 | AssumptionCache *AC) { |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 547 | // Assume undef could be zero. |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 548 | if (isa<UndefValue>(V)) |
| 549 | return true; |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 550 | |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 551 | VectorType *VecTy = dyn_cast<VectorType>(V->getType()); |
| 552 | if (!VecTy) { |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 553 | KnownBits Known = computeKnownBits(V, DL, 0, AC, dyn_cast<Instruction>(V), DT); |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 554 | return Known.isZero(); |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | // Per-component check doesn't work with zeroinitializer |
| 558 | Constant *C = dyn_cast<Constant>(V); |
| 559 | if (!C) |
| 560 | return false; |
| 561 | |
| 562 | if (C->isZeroValue()) |
| 563 | return true; |
| 564 | |
| 565 | // For a vector, KnownZero will only be true if all values are zero, so check |
| 566 | // this per component |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 567 | for (unsigned I = 0, N = VecTy->getNumElements(); I != N; ++I) { |
| 568 | Constant *Elem = C->getAggregateElement(I); |
| 569 | if (isa<UndefValue>(Elem)) |
| 570 | return true; |
| 571 | |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 572 | KnownBits Known = computeKnownBits(Elem, DL); |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 573 | if (Known.isZero()) |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 574 | return true; |
| 575 | } |
| 576 | |
| 577 | return false; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | void Lint::visitSDiv(BinaryOperator &I) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 581 | Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC), |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 582 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | void Lint::visitUDiv(BinaryOperator &I) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 586 | Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC), |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 587 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void Lint::visitSRem(BinaryOperator &I) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 591 | Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC), |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 592 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void Lint::visitURem(BinaryOperator &I) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 596 | Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC), |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 597 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | void Lint::visitAllocaInst(AllocaInst &I) { |
| 601 | if (isa<ConstantInt>(I.getArraySize())) |
| 602 | // This isn't undefined behavior, it's just an obvious pessimization. |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 603 | Assert(&I.getParent()->getParent()->getEntryBlock() == I.getParent(), |
| 604 | "Pessimization: Static alloca outside of entry block", &I); |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 605 | |
| 606 | // TODO: Check for an unusual size (MSB set?) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | void Lint::visitVAArgInst(VAArgInst &I) { |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 610 | visitMemoryReference(I, I.getOperand(0), MemoryLocation::UnknownSize, 0, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 611 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void Lint::visitIndirectBrInst(IndirectBrInst &I) { |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 615 | visitMemoryReference(I, I.getAddress(), MemoryLocation::UnknownSize, 0, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 616 | nullptr, MemRef::Branchee); |
Dan Gohman | d8968da | 2010-08-02 23:06:43 +0000 | [diff] [blame] | 617 | |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 618 | Assert(I.getNumDestinations() != 0, |
| 619 | "Undefined behavior: indirectbr with no destinations", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 622 | void Lint::visitExtractElementInst(ExtractElementInst &I) { |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 623 | if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getIndexOperand(), |
| 624 | /*OffsetOk=*/false))) |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 625 | Assert(CI->getValue().ult(I.getVectorOperandType()->getNumElements()), |
| 626 | "Undefined result: extractelement index out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void Lint::visitInsertElementInst(InsertElementInst &I) { |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 630 | if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getOperand(2), |
| 631 | /*OffsetOk=*/false))) |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 632 | Assert(CI->getValue().ult(I.getType()->getNumElements()), |
| 633 | "Undefined result: insertelement index out of range", &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | void Lint::visitUnreachableInst(UnreachableInst &I) { |
| 637 | // This isn't undefined behavior, it's merely suspicious. |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 638 | Assert(&I == &I.getParent()->front() || |
| 639 | std::prev(I.getIterator())->mayHaveSideEffects(), |
Benjamin Kramer | f027ad7 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 640 | "Unusual: unreachable immediately preceded by instruction without " |
| 641 | "side effects", |
| 642 | &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 645 | /// findValue - Look through bitcasts and simple memory reference patterns |
| 646 | /// to identify an equivalent, but more informative, value. If OffsetOk |
| 647 | /// is true, look through getelementptrs with non-zero offsets too. |
| 648 | /// |
| 649 | /// Most analysis passes don't require this logic, because instcombine |
| 650 | /// will simplify most of these kinds of things away. But it's a goal of |
| 651 | /// this Lint pass to be useful even on non-optimized IR. |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 652 | Value *Lint::findValue(Value *V, bool OffsetOk) const { |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 653 | SmallPtrSet<Value *, 4> Visited; |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 654 | return findValueImpl(V, OffsetOk, Visited); |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /// findValueImpl - Implementation helper for findValue. |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 658 | Value *Lint::findValueImpl(Value *V, bool OffsetOk, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 659 | SmallPtrSetImpl<Value *> &Visited) const { |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 660 | // Detect self-referential values. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 661 | if (!Visited.insert(V).second) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 662 | return UndefValue::get(V->getType()); |
| 663 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 664 | // TODO: Look through sext or zext cast, when the result is known to |
| 665 | // be interpreted as signed or unsigned, respectively. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 666 | // TODO: Look through eliminable cast pairs. |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 667 | // TODO: Look through calls with unique return values. |
| 668 | // TODO: Look through vector insert/extract/shuffle. |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 669 | V = OffsetOk ? GetUnderlyingObject(V, *DL) : V->stripPointerCasts(); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 670 | if (LoadInst *L = dyn_cast<LoadInst>(V)) { |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 671 | BasicBlock::iterator BBI = L->getIterator(); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 672 | BasicBlock *BB = L->getParent(); |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 673 | SmallPtrSet<BasicBlock *, 4> VisitedBlocks; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 674 | for (;;) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 675 | if (!VisitedBlocks.insert(BB).second) |
| 676 | break; |
Larisse Voufo | 532bf71 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 677 | if (Value *U = |
Eduard Burtescu | e2a6917 | 2016-01-22 01:51:51 +0000 | [diff] [blame] | 678 | FindAvailableLoadedValue(L, BB, BBI, DefMaxInstsToScan, AA)) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 679 | return findValueImpl(U, OffsetOk, Visited); |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 680 | if (BBI != BB->begin()) break; |
| 681 | BB = BB->getUniquePredecessor(); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 682 | if (!BB) break; |
| 683 | BBI = BB->end(); |
| 684 | } |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 685 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 686 | if (Value *W = PN->hasConstantValue()) |
Duncan Sands | ec7a6ec | 2010-11-17 10:23:23 +0000 | [diff] [blame] | 687 | if (W != V) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 688 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 689 | } else if (CastInst *CI = dyn_cast<CastInst>(V)) { |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 690 | if (CI->isNoopCast(*DL)) |
| 691 | return findValueImpl(CI->getOperand(0), OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 692 | } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) { |
| 693 | if (Value *W = FindInsertedValue(Ex->getAggregateOperand(), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 694 | Ex->getIndices())) |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 695 | if (W != V) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 696 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 697 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 698 | // Same as above, but for ConstantExpr instead of Instruction. |
| 699 | if (Instruction::isCast(CE->getOpcode())) { |
| 700 | if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 701 | CE->getOperand(0)->getType(), CE->getType(), |
Mikael Holmen | 6efe507 | 2017-10-03 06:03:49 +0000 | [diff] [blame] | 702 | *DL)) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 703 | return findValueImpl(CE->getOperand(0), OffsetOk, Visited); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 704 | } else if (CE->getOpcode() == Instruction::ExtractValue) { |
Jay Foad | 0091fe8 | 2011-04-13 15:22:40 +0000 | [diff] [blame] | 705 | ArrayRef<unsigned> Indices = CE->getIndices(); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 706 | if (Value *W = FindInsertedValue(CE->getOperand(0), Indices)) |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 707 | if (W != V) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 708 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 709 | } |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | // As a last resort, try SimplifyInstruction or constant folding. |
| 713 | if (Instruction *Inst = dyn_cast<Instruction>(V)) { |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 714 | if (Value *W = SimplifyInstruction(Inst, {*DL, TLI, DT, AC})) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 715 | return findValueImpl(W, OffsetOk, Visited); |
David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 716 | } else if (auto *C = dyn_cast<Constant>(V)) { |
| 717 | if (Value *W = ConstantFoldConstant(C, *DL, TLI)) |
| 718 | if (W && W != V) |
Chandler Carruth | 50fee93 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 719 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | return V; |
| 723 | } |
| 724 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 725 | //===----------------------------------------------------------------------===// |
| 726 | // Implement the public interfaces to this file... |
| 727 | //===----------------------------------------------------------------------===// |
| 728 | |
| 729 | FunctionPass *llvm::createLintPass() { |
| 730 | return new Lint(); |
| 731 | } |
| 732 | |
| 733 | /// lintFunction - Check a function for errors, printing messages on stderr. |
| 734 | /// |
| 735 | void llvm::lintFunction(const Function &f) { |
| 736 | Function &F = const_cast<Function&>(f); |
| 737 | assert(!F.isDeclaration() && "Cannot lint external functions"); |
| 738 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 739 | legacy::FunctionPassManager FPM(F.getParent()); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 740 | Lint *V = new Lint(); |
| 741 | FPM.add(V); |
| 742 | FPM.run(F); |
| 743 | } |
| 744 | |
| 745 | /// lintModule - Check a module for errors, printing messages on stderr. |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 746 | /// |
Dan Gohman | 084bcb1 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 747 | void llvm::lintModule(const Module &M) { |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 748 | legacy::PassManager PM; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 749 | Lint *V = new Lint(); |
| 750 | PM.add(V); |
| 751 | PM.run(const_cast<Module&>(M)); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 752 | } |