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