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