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