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